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′.

5 Comments so far

  1. fery on June 28th, 2010

    Thanks alex :)

  2. Marco on August 2nd, 2011

    Too me whole day to find this! Thanks!!

  3. Darren on October 20th, 2011

    Thanks for finding this alex.

  4. Amit on November 16th, 2011

    Thanks dude!!

  5. Bilal on November 16th, 2011

    Thank you

Leave a reply