【发布时间】:2021-12-18 12:02:31
【问题描述】:
Postgres 10.8
我的数据库中有一个 JSON 字段,其中包含价格(如果有的话)。
我得到这样的值:
select t.config::jsonb ->> 'price' as price from mytable t
它以文本格式返回以下结果(空、空和价格):
[null],'',1330.0
我需要能够将此字段设为数字,以便以后可以对该字段求和。
我试过这样解决:
select
(
case
when t.config::jsonb ->> 'price' is null then '0.00'
when t.config::jsonb ->> 'price' = '' then '0.00'
else t.config::jsonb ->> 'price'
end)::decimal as price
from mytable t
这给了我一个数字(131089,0)。我希望该字段像数字(10,2)。一定有其他更简单的方法可以做到这一点吗?
【问题讨论】:
标签: sql json postgresql casting