patternsqlModerate
Repeating pattern X amount of times in LIKE
Viewed 0 times
amountrepeatingliketimespattern
Problem
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:
Is there a shorthand way to do that?
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?
Solution
Yes. Function
REPLICATE():SELECT * FROM table WHERE account LIKE REPLICATE('[0-9]', 5) ;Code Snippets
SELECT * FROM table WHERE account LIKE REPLICATE('[0-9]', 5) ;Context
StackExchange Database Administrators Q#151156, answer score: 11
Revisions (0)
No revisions yet.