1

Is it possible in mysql to replace the first character of a cell with something else?

eg I have

64123
64340
64067
0104
0240
0456

I need to replace all the starting 0's with 64

how could I do this?

2
  • Do you mean replace the first 0 only if it is the first char? Commented Oct 13, 2010 at 3:27
  • That would be correct, I know I can use Replace() to replace ALL zero's but how do I do only the first zero? Commented Oct 13, 2010 at 3:28

1 Answer 1

1
UPDATE action_4_members
SET mobile = CONCAT('64', SUBSTRING(mobile,2))
WHERE SUBSTRING(mobile,1,1) = '0'
Sign up to request clarification or add additional context in comments.

4 Comments

I have attempted UPDATE action_4_members SET mobile = (IF SUBSTRING(mobile,0,1) == "0" THEN CONCAT("64", SUBSTRING(mobile,1)) ELSE mobile); but I get syntax error near 'SUBSTRING(mobile,0,1) == "0" THEN CONCAT("64", SUBSTRING(mobile,1)) ELSE mobile)'
In an UPDATE statement you don't need the IF this should be done in the WHERE clause. I adjusted the code.
My bad, Now I have tried UPDATE action_4_members SET mobile` = CONCAT("64", SUBSTRING(mobile,1)) WHERE SUBSTRING(mobile,0,1) = '0';` But its not updating any rows.
ah fixed it, had to use 0 instead of '0'

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.