I am relatively new to PostgreSQL and I know how to pad a number with zeros to the left in SQL Server but I\'m struggling to figure this out in PostgreSQL.
I have a
You can use the rpad
and lpad
functions to pad numbers to the right or to the left, respectively. Note that this does not work directly on numbers, so you'll have to use ::char
or ::text
to cast them:
SELECT RPAD(numcol::text, 3, '0'), -- Zero-pads to the right up to the length of 3
LPAD(numcol::text, 3, '0'), -- Zero-pads to the left up to the length of 3
FROM my_table