I took a look at the documentation for patterns from Microsoft (can be found here), and from what I understood, it doesn't say a way for a pattern to repeat a limited number of times. It's either the wildcard (which goes on indefinitely) or looking for a specific character in a location, with each location typed out independently. Is there a way to type out "Pattern X repeated Y times"?

For example, if I wanted to look up account numbers that have exactly 5 digits, what I have now is:

SELECT * FROM table WHERE account LIKE '[0-9][0-9][0-9][0-9][0-9]'

Is there a shorthand way to do that?


Yes. Function REPLICATE():

SELECT * FROM table WHERE account LIKE REPLICATE('[0-9]', 5) ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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