1

I have a mysql database column named like telephoneNo

Telephone No 
25645656
45454545
45565656

I want to prepend two digits XX to every value of telephoneNo column

Telephone No 
xx25645656
xx45454545
xx45565656

I was trying to workout with concat but its not with integer values in my case please help with update query

3
  • 2
    Why are these intetgers? Are you planning to subtract one telephone number from another? Commented Feb 17, 2014 at 13:38
  • no i just want to insert 92 in front of all numbers in a coloum. this is my requirement Commented Feb 17, 2014 at 13:39
  • 7
    Telephone numbers are strings. Store them as strings. Commented Feb 17, 2014 at 13:40

3 Answers 3

2

You can use CAST() to convert your integers explicitely:

UPDATE t SET phone=CAST(CONCAT('10', phone) AS UNSIGNED)

That will work with integer prefixes. However, I don't see solid reason to store phone numbers as integers and not strings

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

Comments

1

That's a hack change your col to varchar or something.

UPDATE table SET telephoneNo=9200000000+telephoneNo;

EDIT:

This method requires that all your numbers are of the same length, and 8 digits long, if all your numbers are more or less than 8 digits modify the number of 0's after the 92 accordingly

3 Comments

While this looks good (from operation cost viewpoint) - it requires that all numbers are same length (and also length should be known before UPDATE) - so I think that should be mentioned
I supposed he knows all the lengths and they are all the same as the column is int, and he wants to add 92 to all of the so they are all in the same country or region
Well, even in same country there could be different numeration length (if we're speaking about phone numbers)
0

If your telephone column is int, you can use concat string function to concat with int or string even like below

SELECT CONCAT(xx, telephone);

(OR)

SELECT CONCAT('xx', telephone);

(OR)

Explicitly cast your int column value

SELECT CONCAT('xx', CAST(telephone AS CHAR));

1 Comment

You can't rely on implicit conversions in common case (because strict mode could prohibit that)

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.