2

Just curious that is there any easy way to filter certain string out instead of using the following method:

example: for AccountNumber attribute, that should allow exactly 10 digits as the value, like, 0123456789,

So for the query I made like :

@input like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

I am just wondering is there any alternate way to write this query? for those value which require exact 100 digits, nobody want to count while keep pasting [0-9], right? I notice there is something in C# like ^(\d{10})$, but I cannot find such matching method in TSQL, does this similar method exist?

2 Answers 2

6

Your logic is fine. You can also write this as:

where len(AccountNumber) = 10 and AccountNumber not like '%[^0-9]%'

That is, the length is 10 and it contains no characters that are not digits.

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

Comments

2

You could use

@input like REPLICATE('[0-9]',10) COLLATE Latin1_General_100_BIN2

The explicit collate clause is because in some collations the range will match things that aren't strictly digits.

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.