3

I have the following column in my table

MobilePhone
----------
+1 647 555 5556

I want to end up with the following format.

Basically removing the '+' sign, the country code '1' and all spaces.

MobilePhone
----------
6475555556

can someone please point to right direction.

3
  • Is the country code always 1 character long? Commented Aug 10, 2017 at 17:56
  • REPLACE() function Commented Aug 10, 2017 at 17:57
  • 1
    Try to learn about SUBSTRING & REPLACE functions in SQL Server. Commented Aug 10, 2017 at 17:57

5 Answers 5

3

If they're all US phone numbers, you could try:

select RIGHT(REPLACE(MobilePhone,' ',''),10)
From table
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks LONG, Siyual... that did it!
1

Read past the first space, remove other spaces:

REPLACE(SUBSTRING(MobilePhone, CHARINDEX(' ', MobilePhone, 1), LEN(MobilePhone)), ' ', '')

This assumes your format is strict, if it can be any country code with optional spaces you need another lookup table as they are variable length.

1 Comment

That's a better answer than the one I provided. +1 :)
0

I've nested 2 operations in one UPDATE command. Innermost REPLACE gets rid of the spaces. The RIGHT leaves only 10 characters, that should remove the country code prefix (considering it's US only).

update tbl set MobilePhone= RIGHT(REPLACE(MobilePhone, ' ', ''),10)

Comments

0

All you need is the right and replace function. Irrespective of the country code: Try running this:

declare @a varchar(30) = '+1 510 999 1234'
declare @b varchar(30) = '+91 98318 12345'

SELECT RIGHT(REPLACE(@a,' ',''),10) --5109991234
SELECT RIGHT(REPLACE(@b,' ',''),10) --9831812345

Comments

0
select replace(REPLACE(MobilePhone,' ',''), '+','')
From table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.