Postgres Stored Procedure to Clean Up Phone Numbers
$30-250 USD
Completed
Posted over 13 years ago
$30-250 USD
Paid on delivery
We need stored procedures and an example of how to update an entire table in SQL to do the following:
1) strip out all non-numeric characters from each phone number (i.e., remove all ( and ) and . and - characters plus anything else in there)
2) if the resulting number starts with a 1, remove the 1
A suggested SQL Server based procedure is as follows:
ALTER FUNCTION [dbo].[RemoveChars](@Str varchar(1000))
RETURNS VARCHAR(1000)
BEGIN
declare @NewStr varchar(1000),
@i int
set @i = 1
set @NewStr = ''
while @i <= len(@str)
begin
--grab digits or (| in regex) decimal
if substring(@str,@i,1) like '%[0-9]%'
begin
set @NewStr = @NewStr + substring(@str,@i,1)
end
else
begin
set @NewStr = @NewStr
end
set @i = @i + 1
end
RETURN Rtrim(Ltrim(@NewStr))
END
The second one removes the leading 1 if the phone number begins with a 1 and then checks if the phone number is longer than 10 characters, in which case it trims it to 10:
ALTER FUNCTION [dbo].[Remove1AndCut](@Str varchar(1000))
RETURNS VARCHAR(1000)
BEGIN
declare @NewStr varchar(1000)
--,@i int
--set @i = 1
set @NewStr = ''
--while @i <= len(@str)
--begin
--grab digits or (| in regex) decimal
if substring(@str,1,1) = '1'
begin
set @NewStr = @NewStr + substring(@str,2,len(@str))
end
if len(@str)>10
begin
set @NewStr = substring(@str,1,10)
end
else
begin
set @NewStr = @NewStr
end
--set @i = @i + 1
--end
RETURN Rtrim(Ltrim(@NewStr))
END
This should work on latest PostgreSQL