如何在 PostgreSQL 中将 int 转换为 string

use*_*498 9 postgresql

我有这个:COALESCE(amount ,0) as amount。的数据类型amountnumeric.

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

ERROR:  invalid input syntax for type numeric: "N/A"
Run Code Online (Sandbox Code Playgroud)

Lau*_*lbe 9

使用类型转换:

coalesce(CAST(amount AS text), 'N/A')
Run Code Online (Sandbox Code Playgroud)


Igo*_*ova 1

在你的情况下更适合这个代码: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
Run Code Online (Sandbox Code Playgroud)