0

I have this kind of data.

ID
1899
11999
008999

I need to update the data into this.I just need to fill the '0' or '00' in front and remains if sufficient 6 digits.

ID
001899
011999
008999 
1
  • What is the datatype of id? Commented Jan 2, 2020 at 10:01

2 Answers 2

2

First change Data-type of that column to char

ALTER TABLE `table` CHANGE `id` `id` CHAR(6); 

Then use LPAD()

UPDATE table SET `id`=LPAD(`id`, 6, '0');
Sign up to request clarification or add additional context in comments.

5 Comments

LPAD(TRIM(id), 6, '0'); after CHAR(6) (but indeed VARCHAR is not needed, and INT impossible).
TRIM() will not be required as data is already trimmed when it was stored as INT()
008999 suggests VARCHAR but that could indeed be a copy error.
@JoopEggen Can you post the error message so i can look into it.
congrats; I meant an error in typing the question, in the section "I have this kind of data".
1

I would suggest not doing this update, and not storing your numeric data as text. Instead, if you need to view the numbers with padded zeroes, just handle that when you query, e.g.

SELECT RIGHT(CONCAT('000000', ID), 6) AS ID_padded
FROM yourTable;

Strictly speaking we should also cast ID to text before calling CONCAT, but MySQL should be able to cope with this via implicit conversion.

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.