Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Jun 22

Written by: Steve Gray
6/22/2009 2:33 PM  RssIcon

I’m always having to to massage phone numbers. I prefer to scrub them in the database (remove all the non-numeric characters) and then reformat them on display. Here are my routines for scrubbing them, and then redisplaying the number.

IF EXISTS (SELECT * 
       FROM   sysobjects 
       WHERE  name = N'f_4P_phone')
    DROP FUNCTION f_4P_phone
GO
 
 
CREATE FUNCTION f_4P_phone 
    (@phone  varchar(21))
 
Returns varchar(30)
 
AS
begin
    declare @out varchar(200)
    set @out = ''
    declare @phone2 varchar(20)
    set @phone2 = ''
    declare @pos as int
    set @pos = 0
        
    --remove any special chars
    while @pos < len(@phone) begin
        set @pos = @pos + 1        
    
        if isnumeric(substring(@phone,@pos,1)) > 0 and substring(@phone,@pos,1) <> '-' begin
            set @phone2 = @phone2 + substring(@phone,@pos,1)
        end    
        
    end 
 
    if @phone is null or @phone = '' begin
        return('')
    end
    
    if len(@phone2) >= 10 begin
        set @out = '('
        set @out = @out + substring(@phone2,1,3)
        set @out = @out + ') '
        set @out = @out + substring(@phone2,4,3)
        set @out = @out + '-'
        set @out = @out + substring(@phone2,7,4)
        if substring(@phone2,11,99) > '' begin
            set @out = @out + ' ' + substring(@phone2,11,99)
        end
    
        return (@out)
    end 
    
    if len(@phone2) = 7 begin
        set @out = @out + substring(@phone2,1,3)
        set @out = @out + '-'
        set @out = @out + substring(@phone2,4,4)
    
        return (@out)
    end 
    
    set @out = @phone2
    return (@out)
end
 
go
 
grant all on f_4P_phone to public 
IF EXISTS (SELECT * 
       FROM   sysobjects 
       WHERE  name = N'f_4P_phoneStrip')
    DROP FUNCTION f_4P_phoneStrip
GO
 
 
CREATE FUNCTION f_4P_phoneStrip 
    (@phone  varchar(21))
 
Returns varchar(30)
 
AS
begin
    declare @out varchar(21)
    set @out = ''
    
    declare @pos int
    set @pos = 1
    
    while @pos < len(@phone) + 1 begin
        if substring(@phone,@pos,1) = '-' begin
            set @pos = @pos + 1
            continue
        end
        
        if isnumeric(substring(@phone,@pos,1)) > 0   begin
            set @out = @out + substring(@phone,@pos,1)
        end 
        
        set @pos = @pos + 1
    end
    
 
    return (@out)
end
 
go
 
grant all on f_4P_phoneStrip to public 

Tags:
Categories:
Location: Blogs Parent Separator DEVSHED Blogs Child Separator SQL
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us