26

I have stored values in my database that look like 5XXXXXX, where X can be any digit. In other words, I need to match incoming SQL query strings like 5349878.

Does anyone have an idea how to do it?

I have different cases like XXXX7XX for example, so it has to be generic. I don't care about representing the pattern in a different way inside the SQL Server.

I'm working with c# in .NET.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Jack
  • 667
  • 3
  • 9
  • 13

5 Answers5

32

You can write queries like this in SQL Server:

--each [0-9] matches a single digit, this would match 5xx
SELECT * FROM YourTable WHERE SomeField LIKE '5[0-9][0-9]'
dan
  • 9,712
  • 6
  • 49
  • 62
22

stored value in DB is: 5XXXXXX [where x can be any digit]

You don't mention data types - if numeric, you'll likely have to use CAST/CONVERT to change the data type to [n]varchar.

Use:

WHERE CHARINDEX(column, '5') = 1
  AND CHARINDEX(column, '.') = 0 --to stop decimals if needed
  AND ISNUMERIC(column) = 1

References:

i have also different cases like XXXX7XX for example, so it has to be generic.

Use:

WHERE PATINDEX('%7%', column) = 5
  AND CHARINDEX(column, '.') = 0 --to stop decimals if needed
  AND ISNUMERIC(column) = 1

References:

Regex Support

SQL Server 2000+ supports regex, but the catch is you have to create the UDF function in CLR before you have the ability. There are numerous articles providing example code if you google them. Once you have that in place, you can use:

  • 5\d{6} for your first example
  • \d{4}7\d{2} for your second example

For more info on regular expressions, I highly recommend this website.

rrrr-o
  • 2,447
  • 2
  • 24
  • 52
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
  • 1
    i haven't completely checked it yet, but i looks like a very good answer. i think that you help me a lot. thank you very much! – Jack Dec 26 '09 at 19:47
  • One minor detail, IsNumeric would consider a decimal point valid, so an expresssion of 5123.45 would pass the IsNumeric () check, but might not meet your criteria – Sparky Dec 26 '09 at 20:58
  • @Sparky: True, updated answer with comment to exclude those if necessary. – OMG Ponies Dec 26 '09 at 21:03
  • SQL Server 2005+ include the CLR, 2000 does not. – Michel de Ruiter May 06 '16 at 10:15
  • Would this also work if the pattern `%7%` would appear multiple times or does PATINDEX only return the first occurrence? E.g. `X7XX7XX`, would `PATINDEX` not return 1 instead of 5? – lucidbrot Jun 09 '17 at 08:44
6

Try this

select * from mytable
where p1 not like '%[^0-9]%' and substring(p1,1,1)='5'

Of course, you'll need to adjust the substring value, but the rest should work...

Sparky
  • 14,967
  • 2
  • 31
  • 45
2

In order to match a digit, you can use [0-9].

So you could use 5[0-9][0-9][0-9][0-9][0-9][0-9] and [0-9][0-9][0-9][0-9]7[0-9][0-9][0-9]. I do this a lot for zip codes.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Robert
  • 21
  • 1
0

SQL Wildcards are enough for this purpose. Follow this link: http://www.w3schools.com/SQL/sql_wildcards.asp

you need to use a query like this:

select * from mytable where msisdn like '%7%'

or

select * from mytable where msisdn like '56655%'
JCasso
  • 5,423
  • 2
  • 28
  • 42
  • thanks for reple! these queries will give me many unwanted matches. %7% will match also 3373 which is not a good match for me. 56655% will match also 56655444333 which is again not a good match for me. the query itself has to be with a fixed number. and the DB itself has to have the pattern. is there anything like that? – Jack Dec 26 '09 at 19:41
  • @Jack: yes there is. You may use len function. select * from mytable where msisdn like '56655%' and len(msisdn) = 10 – JCasso Dec 26 '09 at 21:27