Rashedul Hasan Likhon
Rashedul Hasan Likhon

Reputation: 37

How to format numbers in PostgreSQL?

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

For Example:

If the sum is 12345 it will be displayed as 123,45

If the sum is 12345678 it will be displayed as 123,456,78

How to solve it?

Upvotes: 2

Views: 86

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521279

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

Upvotes: 3

Related Questions