【问题标题】:Postgres: update column type and parse column value from json to floatPostgres:更新列类型并将列值从json解析为float
【发布时间】:2020-07-17 14:22:13
【问题描述】:

示例表:

CREATE TABLE example_table (
    example_column TEXT,
)

示例记录(json 存储为文本):

INSERT INTO example_table VALUES ('{"value": "1.3", "Type": "float"}')

目标是:

  1. 将列类型更新为双精度
  2. 将值更新为 1.3

我知道以下内容接近我想要的,但我找不到正确的语法。

ALTER TABLE example_table ALTER COLUMN example_column DOUBLE PRECISION...missing parse syntax

【问题讨论】:

    标签: sql postgresql ddl


    【解决方案1】:

    您可以指定using 子句来定义转换:

    alter table example_table
       alter example_column 
          type double precision using (example_column::jsonb ->> 'value')::double precision
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2015-10-21
      相关资源
      最近更新 更多