Sql-server – Repeating pattern X amount of times in LIKE

likepattern matchingregexsql server

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?

Best Answer

Yes. Function REPLICATE():

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