postgresql

Comma separating numbers without advance knowledge of number of digits in Postgres


In Postgres 9.5:

select to_char(111, 'FM9,999'); -- gives 111
select to_char(1111, 'FM9,999'); -- gives 1,111
select to_char(11111, 'FM9,999'); -- gives #,### !!!

How can I format my numbers with commas without advance knowledge/assumption of maximum number of possible digits preferably by using built-in stuff like above?


Solution

  • Add another digit or the maximum possible value:

    select to_char(11111, 'FM9,999,999');