【问题标题】:SQL Server search using like while ignoring blank spacesSQL Server 使用like 搜索同时忽略空格
【发布时间】:2017-08-14 23:00:26
【问题描述】:

我在数据库中有一个phone 列,记录的右侧包含不需要的空格。我尝试使用修剪和替换,但它没有返回正确的结果。

如果我使用

phone like '%2581254%'

返回

 customerid
 -----------
 33470
 33472
 33473
 33474

但我只需要在开头使用百分号或通配符,我只想匹配左侧。

所以如果我这样使用它

 phone like '%2581254'

我什么也得不到,因为右边的空格!

所以我尝试使用修剪和替换,但只得到一个结果

LTRIM(RTRIM(phone)) LIKE '%2581254'

返回

 customerid
 -----------
 33474

请注意,这四个 id 具有相同的电话号码!

表格数据

customerid    phone
-------------------------------------
33470         96506217601532388254
33472         96506217601532388254
33473         96506217601532388254
33474         96506217601532388254  
33475         966508307940                                                                                                             

我为测试建议添加了很多数字

php 函数取最后 7 位数字并进行比较。

例如

01532388254 will be 2581254

我想搜索电话号码中包含这 7 位数字的所有用户 2581254

我不知道问题出在哪里!

它应该返回 4 个 id 而不是 1 个 id

【问题讨论】:

  • 请贴出问题中的表格数据(尤其是电话号码和列的日期类型)
  • 显然,这三个只是包含2581254,而不是结束。
  • 我添加了我要搜索的数据
  • 您的 PHP 函数是采用最后 7 位数字还是采用空格加上最多 7 个字符的数字?在将空格插入数据库之前修剪空格。
  • 没有任何空格或字符我使用 var_dump php:232:string '2581254' (length=7) 打印电话号码,因为你看到没有空格

标签: sql sql-server database tsql sql-like


【解决方案1】:

鉴于示例数据,我怀疑您的数据中有控制字符。例如char(13)、char(10)

要确认这一点,只需运行以下命令

Select customerid,phone
 From  YourTable
 Where CharIndex(CHAR(0),[phone])+CharIndex(CHAR(1),[phone])+CharIndex(CHAR(2),[phone])+CharIndex(CHAR(3),[phone])
      +CharIndex(CHAR(4),[phone])+CharIndex(CHAR(5),[phone])+CharIndex(CHAR(6),[phone])+CharIndex(CHAR(7),[phone])
      +CharIndex(CHAR(8),[phone])+CharIndex(CHAR(9),[phone])+CharIndex(CHAR(10),[phone])+CharIndex(CHAR(11),[phone])
      +CharIndex(CHAR(12),[phone])+CharIndex(CHAR(13),[phone])+CharIndex(CHAR(14),[phone])+CharIndex(CHAR(15),[phone])
      +CharIndex(CHAR(16),[phone])+CharIndex(CHAR(17),[phone])+CharIndex(CHAR(18),[phone])+CharIndex(CHAR(19),[phone])
      +CharIndex(CHAR(20),[phone])+CharIndex(CHAR(21),[phone])+CharIndex(CHAR(22),[phone])+CharIndex(CHAR(23),[phone])
      +CharIndex(CHAR(24),[phone])+CharIndex(CHAR(25),[phone])+CharIndex(CHAR(26),[phone])+CharIndex(CHAR(27),[phone])
      +CharIndex(CHAR(28),[phone])+CharIndex(CHAR(29),[phone])+CharIndex(CHAR(30),[phone])+CharIndex(CHAR(31),[phone])
      +CharIndex(CHAR(127),[phone]) >0

如果测试结果为阳性

以下 UDF 可用于通过更新从数据中去除控制字符

Update YourTable Set Phone=[dbo].[udf-Str-Strip-Control](Phone)

有兴趣的 UDF

CREATE FUNCTION [dbo].[udf-Str-Strip-Control](@S varchar(max))
Returns varchar(max)
Begin
    ;with  cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
           cte2(C) As (Select Top (32) Char(Row_Number() over (Order By (Select NULL))-1) From cte1 a,cte1 b)
    Select @S = Replace(@S,C,' ')
     From  cte2

    Return LTrim(RTrim(Replace(Replace(Replace(@S,' ','><'),'<>',''),'><',' ')))
End
--Select [dbo].[udf-Str-Strip-Control]('Michael        '+char(13)+char(10)+'LastName')  --Returns: Michael LastName

按照承诺(并由比尔推动),以下是对 UDF 的一点评论。

  1. 我们传递了一个我们想要去除控制字符的字符串
  2. 我们创建一个包含 0 - 31 个 ascii 字符的临时计数表
  3. 然后我们对 理货表。找到的每个字符都将替换为空格
  4. 最后的字符串去掉了重复的空格(一个小技巧 戈登几周前演示过——没有原版 链接)

【讨论】:

  • 查询返回 3 而不是 4 条记录 33470 33472 33473 我不认为它会工作,每天至少添加 100 个电话,我认为数据库添加了这些空格,因为列类型是 nchar 如果我在每次插入后运行它,你的方法会起作用吗?
  • @AhmadzIssa OK 4 条记录中有 3 条具有控制字符。 UDF 用于清理数据...运行一次更新,然后将 UDF 用作增量更新/添加的一部分
  • 谢谢我像这样使用它 SELECT customerid FROM [ccctadm].[customer] WHERE [ccctadm].[udf-Str-Strip-Control](Phone) like '%2581254' 并且工作起来像一个魅力我感谢你的帮助。我真的不明白这个函数的任何内容,但它有效,谢谢
  • @AhmadzIssa 很高兴它有帮助。稍后我会为 udf 添加一些评论。
  • 我以 SO 审稿人的身份写作,因为您的答案已被标记。您现在愿意添加该评论吗? :)
猜你喜欢
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 2016-09-21
  • 1970-01-01
相关资源
最近更新 更多