DB2 SQL Convert Decimal to Character with Padded Zeros

macunte picture macunte · Mar 20, 2013 · Viewed 96.9k times · Source

How can I convert my DECIMAL(11) field from 12345678 to a character value of 00012345678?

Answer

kelgwiin picture kelgwiin · Apr 21, 2015

Only use the DIGITS function, because this verifies the length of the field numeric or decimal, etc and completes with zeros to the left when is necessary.

SELECT DIGITS(FIELD) FROM ...

The length of the resulting string is always:

  • 5 if the argument is a small integer
  • 10 if the argument is a large integer
  • 19 if the argument is a big integer