doudong1117 2013-06-20 15:11
浏览 25
已采纳

使用SQL Server,有没有办法使用5553442524查询(555)344-2524?

Using the PHP SQLSRV driver to connect to SQL Server 2000, is there a way I could match all of these rows using this piece of data: 5553442524?

555-344-2524
(555) 344-2524
555.344.2524
1-555-344-2524

I imagine this would be done through a specific query probably using a stored procedure?

Thank you.

  • 写回答

1条回答 默认 最新

  • duanpa1980 2013-06-20 15:15
    关注

    For SQL 2000 the only way I can think of would be using the REPLACE function.

    declare @SearchTerm bigint
    
    Set @SearchTerm = 5553442524
    
    Select * From dbo.Table 
    Where Replace(Replace(Replace(Replace(Col1,'-',''), '(',''),')',''),'.','')
    = @SearchTerm
    

    The problem with this would be it wouldn't cater for the leading 1.

    A better way would be wrap all this logic in to a function.

    e.g.

    Create Function dbo.fn_FormatTelephoneNumber(@input varchar(100))
    returns bigint
    as begin
    
    
    declare @temp bigint
    
    Set @temp = Replace(Replace(Replace(Replace(@input ,'-',''), '(',''),')',''),'.','')
    
    If Len(@temp) = 11
    begin
    Set @temp = Right(@temp, 10)
    end
    
    return @temp
    
    End
    

    To call the function you would use it like so:

    Select *,
          dbo.fn_FormatTelephoneNumber(YourColumnName) as [FormattedTelephoneNumber]
    From dbo.YourTable
    

    Or to use it in a WHERE clause:

    Select *
    From dbo.YourTable
    Where dbo.fn_FormatTelephoneNumber(YourColumnName) = 5553442524
    

    Obviously the best thing here would be to clean up the data that is stored in the columns and restrict any further "bad" data from being inserted. Although in my experience that is easier said than done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?