简体   繁体   English

SQL Server中的数字范围的通配符

[英]Wildcard of Number Ranges in SQL Server

The pattern match in sql server doesn't work like regex. sql server中的模式匹配不像regex那样工作。 Is it possible to use LIKE to match number ranges? 是否可以使用LIKE来匹配数字范围? 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". 我想从“ABCD 1”到“ABCD 20”中选择所有数据。 But I want to ignore anything between "ABCD 4" and "ABCD 8". 但我想忽略“ABCD 4”和“ABCD 8”之间的任何内容。 Of course I can do OR conditions, but I just want to check if there is more elegant way. 当然我可以做OR条件,但我只想检查是否有更优雅的方式。

TIA. TIA。

You can use the LIKE operator and specify your pattern executing a query like this one: 您可以使用LIKE运算符并指定执行如下查询的模式:

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 . 看看这个SQL小提琴

You could use like in SQL Server like this: 你可以在SQL Server中使用像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM