Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL如何在文本字段中搜索第2个而不匹配第22个,等等?_Sql_Sql Server - Fatal编程技术网

SQL如何在文本字段中搜索第2个而不匹配第22个,等等?

SQL如何在文本字段中搜索第2个而不匹配第22个,等等?,sql,sql-server,Sql,Sql Server,我想查询可以出现在文本列中任何位置的编号街道名称,并筛选出具有更多数字的编号街道名称的匹配项,即2nd,但不42nd,182nd,等等。有没有比以下组合更优雅或更简化的方法: WHERE col LIKE '2nd%' OR col LIKE '% 2nd%' 只要2nd没有出现在字符串的开头,就可以使用 col LIKE '%[^0-9]2nd%' 例如: select col, case when col like '%[^0-9]2nd%' then 'second' else 'n

我想查询可以出现在文本列中任何位置的编号街道名称,并筛选出具有更多数字的编号街道名称的匹配项,即
2nd
,但不
42nd
182nd
,等等。有没有比以下组合更优雅或更简化的方法:

WHERE col LIKE '2nd%' OR col LIKE '% 2nd%'

只要
2nd
没有出现在字符串的开头,就可以使用

col LIKE '%[^0-9]2nd%'
例如:

select col, case when  col like '%[^0-9]2nd%' then 'second' else 'not' end as test
from (values ('12 2nd st'), ('45 42nd st'), ('128 22nd st')) test(col)
输出:

col             test
12 2nd st       second
45 42nd st      not
128 22nd st     not

Nick的答案很好,但它不能处理字符串开头出现
'2nd'
的情况。通过在要比较的列上预挂起一个字符,可以轻松处理此问题:

' ' + col LIKE '%[^0-9]2nd%'

仅供参考,第二个参数的第一个%后面有一个空格字符。只有-where地址像'2nd%'有什么问题?@spikeikenbery。您的查询使用SQL Server语法约定(这是标准的)。我删除了MS访问标签。