5

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?

Salmononius2
  • 441
  • 2
  • 6
  • 15

1 Answers1

11

Yes. Function REPLICATE():

SELECT * FROM table WHERE account LIKE REPLICATE('[0-9]', 5) ;
ypercubeᵀᴹ
  • 99,450
  • 13
  • 217
  • 306