Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pad varchar numbers with 0s in db2

Tags:

padding

db2

Is there a way to pad 0s before numbers stored as VARCHAR in DB2?

Like this:

some_column     result
-----------     ------
12          ==>  00012
123         ==>  00123
6454        ==>  06454
like image 808
DateTime Avatar asked Feb 11 '11 00:02

DateTime


1 Answers

If the function LPAD is available:

SELECT LPAD(some_column, 5, '0')
FROM table

Otherwise you can use a combination of RIGHT and REPEAT:

SELECT RIGHT(REPEAT('0', 5) || some_column, 5)
FROM table

some_column  |  Concatenate five '0's to some_column  | Return the five rightmost characters
------------------------------------------------------------------------
    12       =>             0000012                   =>   00012
   123       =>            00000123                   =>   00123
  6454       =>           000006454                   =>   06454
like image 103
Stradivariuz Avatar answered Sep 20 '22 02:09

Stradivariuz