首页 > 解决方案 > 在 Postgresql 10.8 中,如何将 char 类型字段更改为 json 类型,然后获取 json 字符串中的数字?

问题描述

Postgresql 版本是 10.8

这是sql:

update nt_order set common_field='{"bind_channel":"company","bind_status":"binding","version":1234}' where id=1 and (common_field is null or (common_field::json->>'version')::bigint is null or (common_field::json->>'version')::bigint < 12345);

执行此sql时,出现错误:

ERROR:  invalid input syntax for type json
DETAIL:  The input string ended unexpectedly.
CONTEXT:  JSON data, line 1

如果Postgresql版本是9.5,执行上面的sql没有问题。

那么如何在 Postgresql 10.8 中解决这个问题呢?

标签: jsonpostgresqlpostgresql-10

解决方案


根据详细信息和上下文,我会说您的字段中有某行的空字符串。这不能转换为 json,即使在 9.5 版本中也不行,因此您的 9.5 服务器中必须有不同的数据。

大概你想对 '' 做同样的事情,就像你对 NULL 做的一样,所以只需将该测试添加到你的 OR 列表中。


推荐阅读