mpg mpg - 5 months ago 22
SQL Question

Adding a leading zero to some values in column

I have a csv file sent to me in csv. The field of interest is 8 digits. Some of those started with a 0. The field was sent numeric. So, I now have dropped some leading zeros.

I already converted the field to varchar. I now need to do this:

I have this now:

12345678

1234567

I need to have this:

12345678

01234567

I am using mysql.

Thanks

Answer

Change the field back to numeric and use ZEROFILL to keep the zeros

or

use LPAD()

SELECT LPAD('1234567', 8, '0');