0

I want to return all rows where columnX has 10 digits of numerics and nothing else (e.g. '1234567890'). The column in question has other formatted data such as 4 alphas and 4 numerics (e.g. ABCD1234), etc.

I want to utilize my regex in SQL Server 2012 queries.

2 Answers 2

1

SQL Server REGEX uses the LIKE clause with % but for your case you can do this:

SELECT columX
FROM TableT
WHERE ISNUMERIC(columnX) = 1 and LEN(C1) = 10
Sign up to request clarification or add additional context in comments.

3 Comments

thanks - another great workaround. Can you expand on what you mean by using the Like clause for regex? I know the basic use of the like clause, but are you saying there is a way to write more advanced Regex into a like clause?
@ColinMac sorry, i see how what i said could be confusing. LIKE is the closest thing you'll get to REGEX without a lengthy CASE statement or something similar. LIKE can use some REGEX type characters such as _ % [] and ^ but you can't use it like you can other languages such as Java. So generally speaking you can so "No Regex in SQL Server" as you did.
*can say "No Regex in SQL Server"
1

There is no regexp in sql server.

You can use try_cast which returns null when there are any alphabets in the column when you cast it to an bigint.

select * from tablename
where try_cast(columnX as bigint) is not null

Edit: To check if the column length = 10 add one more condition to the where clause.

select * from tablename
where try_cast(columnX as bigint) is not null and len(columnX) = 10

Comments

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.