Add comma after specified length for number format

Viewed 35
( SELECT SUM(misyukko.dataint11) 
   FROM misyukko 
   where syouhinid='$kokyakuorderbango'
) AS total_amount

If sum is 1234 then should be displayed 123,4

If sum is 12345 then should be displayed 123,45

If sum is 12345678 then should be displayed 123,456,78

How to solve it?

1 Answers

You could use REGEXP_REPLACE here:

WITH yourTable AS (
    SELECT 12345 AS num
)

SELECT RTRIM(REGEXP_REPLACE(num::text, '(\d{3})', '\1,', 'g'), ',') AS num_out
FROM yourTable;

screen capture from demo link below

Demo

Related