Padding zeros to the left in postgreSQL

后端 未结 3 1687
滥情空心
滥情空心 2020-12-12 23:24

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

3条回答
  •  囚心锁ツ
    2020-12-12 23:41

    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
    

提交回复
热议问题