0

I have a contact number field which stores number as countrycode + ' ' + phonenumber.. Now i want to strip leading zeroes from phone number

I tried using

UPDATE [dbo].[User]
SET PhoneNumber = REPLACE(LTRIM(REPLACE([PhoneNumber], '0', ' ')), ' ', '0')

but this replaces the space in between with '0'

Any suggestions?

TechDo
  • 18,398
  • 3
  • 51
  • 64
Amey Khadatkar
  • 414
  • 3
  • 16

3 Answers3

1

Try converting the value to int or numeric

Eg:

select '91 004563' as Input, CONVERT(INT, SUBSTRING('91 004563',CHARINDEX(' ','91 004563')+1,100)) as Output

This gives the result

Input       Output
---------   ------
91 004563   4563
Nisha
  • 1,379
  • 16
  • 28
  • a number is like "91 009494949494" I need to remove '00' from the phone number "009494949494"..So this won't work – Amey Khadatkar Sep 23 '13 at 09:43
  • 1
    try to extract the part after space and convert to `BIGINT`. Then convert back to `VARCHAR` and concatenate. I have edited my post. – Nisha Sep 23 '13 at 09:44
1

Try this: SUBSTRING(PhoneNumber, PATINDEX('%[^0 ]%', PhoneNumber + ' '), LEN(PhoneNumber))

Milen
  • 8,697
  • 7
  • 43
  • 57
Pradeeshnarayan
  • 1,235
  • 10
  • 21
0

Try:

declare @PhoneNumber varchar(max) = '00000000000000000000001200000031'
while substring(@PhoneNumber,1,1)='0' 
begin
    set  @PhoneNumber = SUBSTRING(@PhoneNumber,2,LEN(@PhoneNumber))
end

select @PhoneNumber

Addressing your comment:

declare @PhoneNumber varchar(max) = '91 00000000000000000000001200000031'
declare @tempphn varchar(max) = substring(@PhoneNumber,4,len(@PhoneNumber)  )
while substring(@tempphn,1,1)='0' 
begin
    set  @tempphn = SUBSTRING(@tempphn,2,LEN(@tempphn))
end

select @tempphn
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • Well the solutions posted are already known - i wanted to post something different. User did not mention any performance issues. – Sadique Sep 23 '13 at 09:57