1

On my field phone I have like these values:

Phone: 1234567

What I want to achieve is to replace the Phone: (there is a space after : so I can be only with the number. I tried the following without luck

SELECT REPLACE( phone,  'Phone: ',  '' ) 
FROM EVENTS
5
  • I have to ask why in column called phone would you save the data with text in there anyway? Commented Jul 29, 2013 at 13:01
  • @Dale you have a nice question there, however I had to have text in there for a reason. Commented Jul 29, 2013 at 13:02
  • Is Phone: on all results or just some? Commented Jul 29, 2013 at 13:04
  • @Kaoukkos That query works for me. What specific issue are you having? Commented Jul 29, 2013 at 13:04
  • Try to remove the leading space from the data by using LTRIM. Commented Jul 29, 2013 at 13:08

5 Answers 5

1

I usually rename the returned value as a new field, in this case phone_replaced:

SELECT REPLACE(`phone`, 'Phone: ', '') as `phone_replaced` FROM `events`

But you can also keep the current name phone:

SELECT REPLACE(`phone`, 'Phone: ', '') as `phone` FROM `events`

As long as you alias it using as.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 You don't actually have to rename it, but you must alias it at least or it's actually returned as REPLACE(column, 'blah: ', '') and that's just a nightmare to type
True, I've added it to my answer.
1

This actually works:

create table T (data char(40));
insert into T values
 ('Phone: 1235678'),
 ('Tel: 0505074455'),
 ('Phone: 0418192244');

select REPLACE(data, 'Phone: ','') from T;

Result:

REPLACE(DATA, 'PHONE: ','')
1235678
Tel: 05-05-07-44-55
0418192244

If you only want "matching" lines, you should add a relevant where clause:

select REPLACE(data, 'Phone: ','') from T
  where data LIKE 'Phone: %';

If you really want numbers you might use CAST ... as DECIMAL

select CAST(REPLACE(data, 'Phone: ','') as DECIMAL) from T
  where data LIKE 'Phone: %';

If you have to deal with case, try normalizing the string using LOWER:

select CAST(REPLACE(LOWER(data), 'phone: ','') as DECIMAL) from T
  where LOWER(data) LIKE 'phone: %';

That later would not perform well because MySQL won't be able to use the potential index on data I think. If you need, you should use case insensitive collation for the datacolumn. But that's an other story...

http://sqlfiddle.com/#!2/38218d/7

Comments

0

You can achieve it by using the PHP. Use it:

$phone = "Phone: 1234567"
$arr = explode(" ",$phone);
$phone = $arr[1];

Or use the SQL syntax:

SELECT REPLACE("Phone: 1234567",  'Phone: ',  '' ) FROM EVENTS

2 Comments

This will work for only one phone number, however I have numerous rows with phones
@Kaoukkos I have given the other solution also.
0

You try :-

SELECT LTRIM(phone) FROM EVENTS;

Read more about LTRIM.

For Replaceing :-

SELECT REPLACE(phone,phone,LTRIM(phone)) FROM EVENTS;

Hope it will help you.

Comments

0

make sure that names are case sensitive here so Phone: is not same as phone:

     SELECT case when phone like 'phone%' then REPLACE( phone,  'phone: ',  '' ) 
                 when phone like 'Phone%' then REPLACE( phone,  'Phone: ',  '' )
            end as Phone_number
     FROM EVENTS

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.