4

I would like to update the first char of a column in MYSQL

WHERE the format of the string is LIKE '1-%'

change the first char to '0'.

I have created this example and think I am on the right tracks...

UPDATE products 
SET 
   has_roundel = REPLACE(has_roundel, SUBSTRING(has_roundel, 0, 1),'0')
WHERE
   has_roundel like '1-%';
2
  • 2
    ok so what is the problem? Error? Commented Mar 6, 2014 at 11:00
  • It sounds as though you may be trying to manipulate badly denormalised data... Commented Mar 6, 2014 at 11:05

4 Answers 4

4

MySQL SUBSTRING isnt starting from 0.

You can use:

UPDATE products 
SET 
   has_roundel = REPLACE(has_roundel, SUBSTRING(has_roundel, 1, 1),'0')
WHERE
   has_roundel like '1-%'; 

Or:

UPDATE products 
SET 
   has_roundel = REPLACE(has_roundel, SUBSTRING(has_roundel, 1),'0')
WHERE
   has_roundel like '1-%'; 
Sign up to request clarification or add additional context in comments.

Comments

1

Try the following:

update products
set
    has_roundel = concat('0', substring(has_roundel, 2))
where
    has_roundel like '1-%';

Comments

1

Try this

UPDATE products 
SET 
   has_roundel = REPLACE(has_roundel, LEFT(has_roundel,1),'0')
WHERE
   has_roundel like '1-%';

Or:

 UPDATE products 
    SET 
       has_roundel = Concat('0',SUBSTR(data, 2))
    WHERE
       has_roundel like '1-%';

Comments

0

One way is to make use of CONCAT

UPDATE products 
SET 
   has_roundel = CONCAT('0-', substring(has_roundel, 3))
WHERE
   has_roundel like '1-%';

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.