如何在PostgreSQL中将int转换为字符串

时间:2018-07-23 12:49:21

标签: postgresql

我有这个:COALESCE(amount ,0) as amountamount的数据类型为numeric

我想要0而不是N/A。我怎样才能做到这一点?我遇到错误:

ERROR:  invalid input syntax for type numeric: "N/A"

2 个答案:

答案 0 :(得分:1)

使用类型转换:

coalesce(CAST(amount AS text), 'N/A')

答案 1 :(得分:0)

您更适合以下代码:coalesce(cast(nullif(amount, 0) as text), 'N/A')

有关保存的其他信息:

case 
  when coalesce(cast(nullif(a.amount1, 0) as text), 'N/A') = 'N/A' then 'N/A' 
  when coalesce(cast(nullif(a.amount2, 0) as text), 'N/A') = 'N/A' then 'N/A'  
  else (coalesce(cast(nullif(a.amount1 - a.amount2, 0) as text), 'N/A')) 
end as savings