The pattern match in sql server doesn't work like regex. Is it possible to use LIKE to match number ranges? I need to do something like this:
ABCD 1
ABCD 2
ABCD 3
ABCD 4
ABCD 8
ABCD 9
ABCD 10
...
ABCD 20
I want to select all data from "ABCD 1" to "ABCD 20". But I want to ignore anything between "ABCD 4" and "ABCD 8". Of course I can do OR conditions, but I just want to check if there is more elegant way.
TIA.
You can use the LIKE operator and specify your pattern executing a query like this one:
SELECT mydata FROM mytable
WHERE(mydata LIKE 'ABCD [1-9]' OR mydata LIKE 'ABCD 1[0-9]' OR mydata LIKE 'ABCD 20')
AND mydata NOT LIKE 'ABCD [4-8]';
or, something more concise and shorter:
SELECT mydata FROM mytable
where mydata like 'ABCD [^4-8]%';
Have a look at this SQL Fiddle .
You could use like in SQL Server like this:
where col like 'ABCD [1-9]' or
col like 'ABCD 1[0-9]' or
col like 'ABCD 20'
select * from tablename
where cast(replace(somecolumn, 'ABCD','') as numeric) < 4
and cast(replace(somecolumn, 'ABCD','') as numeric) > 8
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.