Oracle, adding leading zeros to string (not number)

Ovi picture Ovi · May 22, 2014 · Viewed 83.5k times · Source

I am using Oracle (work space is TOAD) and I need to make my strings that if they are shorted then 10 characters then add leading zeros to make them all 10 digit strings.

For example if I have a string like this: '12H89' need to be '0000012H89' or '1234' to be '0000001234'

How can this be done? Whats the best way?

Thanks in advance .

Answer

GolezTrol picture GolezTrol · May 22, 2014

You can use the LPAD function for that, passing in the string, the length you want it to be, and the character to pad it with. For 10 digits with leading zeroes this would be:

LPAD('12H89', 10, '0')

The return value is the padded string.

See: http://www.techonthenet.com/oracle/functions/lpad.php