skip to Main Content

I am trying to search a MS SQL database column for 6 numbers. The data in the column is a string of characters example: ab01234555cd0122abc987654efg

Using RegEx : [0-9]{6}

Results are : 012345 and 987654

Here is my current MS SQL code:

SELECT * FROM users WHERE seo LIKE '%[0-9]{6}%'

The {6} does not work.

How can I use a regex qualifier to match the characters count?

2

Answers


  1. SQL Server doesn’t actually have REGEX functionality. You would need to repeat the pattern six times:

    SELECT * FROM users WHERE seo LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%'
    
    Login or Signup to reply.
  2. SQL Server LIKE is does not handle regular expressions, though it can do pattern matching. Since regexp is not supported, you cannot do things like [0-9]{6}. Instead, you need to repeat your pattern, which would look like this: [0-9][0-9][0-9][0-9][0-9][0-9].

    So your query would be:

    SELECT * FROM users WHERE seo LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search