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.
This question is related to
sql
sql-server
regex
expression
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.
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%'
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]'
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...
Source: Stackoverflow.com