使用逗号将函数的数字结果转换为字符串的示例

时间:2017-06-15 17:27:50

标签: sql postgresql

我尝试将TOTAL_MONEY格式化为“123,456,789”(带逗号)。

select entity_id,
       'the sum is:' || to_char(TOTAL_MONEY, ',') as text_msg
from(
select entity_id,
       sum(volume) as TOTAL_MONEY
from procurement) t

但它不起作用。我没有发现here有用的东西。

你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

    select entity_id,
    'the sum is:' || ltrim(rtrim(to_char(TOTAL_MONEY, '999,999,999,999'))) as text_msg
    from(
          select entity_id,
          sum(volume) as TOTAL_MONEY
          from procurement
        ) t

答案 1 :(得分:1)

又一个更简单的解决方案:

select
  entity_id,
  to_char(TOTAL_MONEY, '"the sum is: "FM999,999,999,999') as text_msg
from(
  select entity_id, sum(volume) as TOTAL_MONEY
  from procurement) t

'FM'选项可以删除不必要的空格; "double-quoted string"按原样打印,不替换任何模式/占位符。