Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL removing spaces, non-numeric and second character from string

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.

like image 970
Hector Marcia Avatar asked Sep 12 '26 00:09

Hector Marcia


2 Answers

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

select RIGHT(REPLACE(MobilePhone,' ',''),10)
From table
like image 133
LONG Avatar answered Sep 13 '26 13:09

LONG


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.

like image 38
Alex K. Avatar answered Sep 13 '26 13:09

Alex K.