【问题标题】:Update to remove double quotes from number jsonb postgresql更新以从数字 jsonb postgresql 中删除双引号
【发布时间】:2020-07-27 13:36:41
【问题描述】:

我正在使用 PostgreSQL,我正在尝试为 jsonb 运行更新。

我想将“2W”改为数字 2。

因此,下面的查询删除了 W,但将其保留为“2”。

我将如何删除双引号?

目前它看起来像 {"size": "2W"},我希望它看起来像 {"size": 2}

UPDATE x
SET x = jsonb_set(x, '{size}', ('"' || replace(x->>'size', 'W', '') || '"')::jsonb)
WHERE x IN ('')


【问题讨论】:

    标签: sql json postgresql select jsonb


    【解决方案1】:

    您可以为此使用to_jsonb()

    jsonb_set(x, '{size}', to_jsonb(replace(x->>'size', 'W', '')::int))
    

    Demo on DB Fiddle

    select jsonb_set(x, '{size}', to_jsonb(replace(x->>'size', 'W', '')::int))
    from (values('{"size": "2W"}'::jsonb)) as t(x)
    
    | jsonb_set | | :------------ | | {“大小”:2} |

    如果需要,您可以将 ::int 替换为 ::numeric 以处理十进制值。

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多