1

I have one table. In that I am storing user details there like firstname, lastname, phonenumber. while storing phone number it stores data without extension like +1(013)691-1535x___ . Sometime user might enter extension also that it will store like +1(013)691-1535x12345. Here my doubt is how to retrieve only phone number if extension is in empty. If extension is avaiable I need to view phone number with extension.

4
  • did you store extension in separated column Commented Dec 30, 2013 at 10:18
  • No I am storing in same column. Commented Dec 30, 2013 at 10:26
  • followed by X you should be stored the value of extension ah Commented Dec 30, 2013 at 10:29
  • Yes. I am storing extension followed by X Commented Dec 30, 2013 at 10:33

2 Answers 2

2

You can split the phone number using SUBSTRING_INDEX :

SELECT phone_number,SUBSTRING_INDEX(phone_number, 'x', 1) as phone_num, SUBSTRING_INDEX(phone_number, 'x', -1) as phone_ext FROM tablename;

Query result

+-----------------------+-----------------+-----------+
| phone_number          | phone_num       | phone_ext |
+-----------------------+-----------------+-----------+
| +1(013)691-1535x12345 | +1(013)691-1535 | 12345     |
+-----------------------+-----------------+-----------+

Now, you have entire phone number with extension, phone number without extension and only extension. Depending on the query result, you can have a 'check' in your script and use the field value.

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

1 Comment

I have to see both in the same result. if extension is available it should show, otherwise no need . I donot want results in splited columns. thanks
1

try like or Well if you know the order of your words.. you can use REGEXP

  SELECT `phone_number` FROM `table` WHERE `phone_number` LIKE '+1(013)691-1535x%'

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.