Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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 如何快速比较多个字符串?_Sql_Sql Server_Regex_Database_Tsql - Fatal编程技术网

Sql 如何快速比较多个字符串?

Sql 如何快速比较多个字符串?,sql,sql-server,regex,database,tsql,Sql,Sql Server,Regex,Database,Tsql,在SQL Server中,我有一个包含数字的字符串列。我需要的每个条目只有一个数字,因此不需要解析。我需要一些方法来查找包含400到450的数字的所有行。而不是做: ...where my stringcolumn like '%400%' or stringcolumn like '%401%' or stringcolumn like '%402%' or ... 有没有更好的方法可以节省打字 这些行中还有其他值,例如:“5335154”,test4559@me.com', '555-55

在SQL Server中,我有一个包含数字的字符串列。我需要的每个条目只有一个数字,因此不需要解析。我需要一些方法来查找包含400到450的数字的所有行。而不是做:

...where my stringcolumn like '%400%' or stringcolumn like '%401%' or stringcolumn like '%402%' or ...
有没有更好的方法可以节省打字


这些行中还有其他值,例如:“5335154”,test4559@me.com', '555-555-5555'. 需要考虑将这些过滤掉。

使用regex来完成这项工作

...where stringcolumn like '4[0-4][0-9]' OR stringcolumn like '450'

使用正则表达式来实现这一点

...where stringcolumn like '4[0-4][0-9]' OR stringcolumn like '450'
单程

WHERE Column like '%4[0-4][09]%'
OR Column LIKE '%500%'
请记住,这将选择任何包含数字的内容,因此5000也将以单向方式返回

WHERE Column like '%4[0-4][09]%'
OR Column LIKE '%500%'
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn = '450'
请记住,这将选择任何包含数字的内容,因此也将返回5000

...where stringcolumn like '4[0-4][0-9]' OR stringcolumn = '450'
如果要限制为3位数,则不需要通配符


如果要限制为3位数字,则不需要通配符。

我将执行以下操作:

select t.*
from (select t.*,
             (case when charindex('4', col) > 0
                   then substrint(col, charindex('4', col), charindex('4', col) + 2)
              end) as col4xx
      from t
     ) t
where (case when isnumeric(col4xx) = 1
            then (case when cast(col4xx as int) between 400 and 450 then 'true'
                  end)
       end) = 'true'
我不喜欢在WHERE子句中使用case语句。但是,为了确保转换为数字,需要这样做(或者转换可能成为另一个子查询中的列)。请注意,以下内容并不等同:

where col4xx between '400' and '450'

因为字符串“44A”将匹配。

我将执行以下操作:

select t.*
from (select t.*,
             (case when charindex('4', col) > 0
                   then substrint(col, charindex('4', col), charindex('4', col) + 2)
              end) as col4xx
      from t
     ) t
where (case when isnumeric(col4xx) = 1
            then (case when cast(col4xx as int) between 400 and 450 then 'true'
                  end)
       end) = 'true'
我不喜欢在WHERE子句中使用case语句。但是,为了确保转换为数字,需要这样做(或者转换可能成为另一个子查询中的列)。请注意,以下内容并不等同:

where col4xx between '400' and '450'

因为字符串“44A”会匹配。

字符串看起来像什么?您想匹配1401和401吗?要匹配14010吗?字符串是什么样子的?您想匹配1401和401吗?你想配14010吗?谢谢。有没有办法将其限制为纯3位数?它还收集了其他东西,比如“5335154”,test4559@me.com', '555-555-5555'. 应该在问题中更清楚地说明这一点。我已经更新了问题。谢谢。有没有办法将其限制为纯3位数?它还收集了其他东西,比如“5335154”,test4559@me.com', '555-555-5555'. 应该在问题中更清楚地说明这一点。我已经更新了问题,我想这就是问题所在。谢谢。我想就是这个。谢谢