SQL Server 2000: how do I return only the number from a phone number column

前端 未结 3 2021
耶瑟儿~
耶瑟儿~ 2021-01-19 16:49

I\'m trying to strip out the \"(\", \")\", \"-\", \"x\" or \"X\" \"ext\" and spaces from each phone number so I am left with only the first 10 digits or 1st 10 numbers of a

3条回答
  •  醉酒成梦
    2021-01-19 17:46

    I think your only option is to create a UDF that does it. If you were using 2005+ you could create a CLR function to do it.

    UDF:

    create function dbo.RemoveNonNumericChar(@str varchar(500))  
    returns varchar(500)  
    begin  
    declare @startingIndex int  
    set @startingIndex=-1 
    while @startingIndex <> 0 
    begin  
        set @startingIndex= patindex('%[^0-9]%',@str)  
        if @startingIndex <> 0  
        begin  
            set @str = replace(@str,substring(@str,@startingIndex,1),'')  
        end   
    end  
    return @str  
    end
    
    go  
    
    select dbo.RemoveNonNumericChar('(555) 555-5555 ext55555')  
    

提交回复
热议问题