### MySQL: Add Leading Zeros

There might be many ways to force leading zeros to be displayed when string length is less than expected.

One of them is to use string function LPAD(). For example, the following query:

SELECT LPAD('1101', 8, '0'); |

returns **‘00001101’**.

However, if string (first parameter) is longer than length (second parameter), the return value is shortened. For example:

SELECT LPAD('1101', 2, '0'); |

returns **’11’**.

Thanks alex ðŸ™‚

Too me whole day to find this! Thanks!!

Thanks for finding this alex.

Thanks dude!!

Thank you

Thanks….

Thanks pal.. This is really works..

Thanks you so mutch

Thank you Sir Alex,

This has really been a great help…

Hi Alex,

So is that anyway for if string (first parameter) is longer than length (second parameter) then it will return the first parameter instead of shortened value?

There is always a way for a work around like this:

or this:

Cool. Thanks a bunch.

Thanks so much!

lpad() – just what I was looking for.

Thanks a bunch!

perfect ðŸ™‚

thanks!!

Thank you Alex for the GREATEST() example. Just what i needed!