2

I have special selection criterion for particular column and need the regular epxression for that. e.g: A123456, S008942 this id's should get selected only The first letter is alphabet and next 6 letters are digits. It will always be like total of 7 letters (1Alphabet+6NumericDigits)

Any help is appreciated. Thanks

1

3 Answers 3

3

Using SQL server pattern matching: http://msdn.microsoft.com/en-us/library/ms187489%28SQL.90%29.aspx

SELECT *
FROM [table]
WHERE [column] LIKE '[A-Z][0-9][0-9][0-9][0-9][0-9][0-9]'

It is unfortunately limited, but maybe it will get what you need?

0
1

try this maybe it helps

"^[A-Z]{1}[0-9]{6}$"
7
  • it defines for only one letter
    – COLD TOLD
    Apr 27, 2012 at 5:51
  • SELECT * FROM tblUserData WHERE ID Like '^[A-Za-z]\d{6}$' SELECT * FROM tblUserData WHERE ID Like '^[A-Za-z]{1}[0-9]{6}$' This is also not working :(
    – Pratik
    Apr 27, 2012 at 5:56
  • Both the above queries are not working for me...i am using SQL server 2008
    – Pratik
    Apr 27, 2012 at 5:58
  • 2
    SQL SERVER 2008 does not supports regular expressions
    – COLD TOLD
    Apr 27, 2012 at 6:00
  • Its a simple LIKE expression i am writing ...it should work atleast
    – Pratik
    Apr 27, 2012 at 6:03
0

This is your pattern: [A-Z]\d{6}
If first letter can be lowercase pattern is: [A-Za-z]\d{6}

[A-Z] means an uppercase letter.
[A-Za-z] means an uppercase or lowercase letter.
\d means a digit.
{6} after it means exactly 6 times. (so \d{6} means 6 digits)

7
  • Thanks for your response Ademiban...but its not working in the SQL query SELECT * FROM tableUserdata WHERE ID Like '[A-Z]\d{6}'
    – Pratik
    Apr 27, 2012 at 5:47
  • Are you sure...? try to put ^ on the start and $ at the end: ^[A-Z]\d{6}$
    – shift66
    Apr 27, 2012 at 5:51
  • This is also not working. Any conversion do i need to do before i execute this query
    – Pratik
    Apr 27, 2012 at 5:55
  • I haven't seen your code and can't say why it's not working but this pattern is 100% right. You can check it here: regexpal.com
    – shift66
    Apr 27, 2012 at 5:59
  • Yes i am also sure on pattern .. there is not code actually i needed this for select statement in SQL Server 2008
    – Pratik
    Apr 27, 2012 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.