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?

Sign up to request clarification or add additional context in comments.

Comments

1

try this maybe it helps

"^[A-Z]{1}[0-9]{6}$"

7 Comments

it defines for only one letter
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 :(
Both the above queries are not working for me...i am using SQL server 2008
SQL SERVER 2008 does not supports regular expressions
Its a simple LIKE expression i am writing ...it should work atleast
|
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 Comments

Thanks for your response Ademiban...but its not working in the SQL query SELECT * FROM tableUserdata WHERE ID Like '[A-Z]\d{6}'
Are you sure...? try to put ^ on the start and $ at the end: ^[A-Z]\d{6}$
This is also not working. Any conversion do i need to do before i execute this query
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
Yes i am also sure on pattern .. there is not code actually i needed this for select statement in SQL Server 2008
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.