在 snowflake 中转换 OBJECT_CONSTRUCT 内的数据类型
Converting data type inside OBJECT_CONSTRUCT in snowflake
我正在使用 Snowflake 的 Object_Construct 创建一个包含值的 JSON 数组。
示例:
Select OBJECT_CONSTRUCT(
user_id, user.id::TIMESTAMP,
join_date, user.create_date
)
from user
Select object_construct(*) from above_table
这给我的输出如下
|-------------------------------------------|
| OBJECT_CONSTRUCT(*) |
|-------------------------------------------|
| { |
| "user_id": 1, |
| "join_date": "2021-06-18 19:51:19.000" |
| } |
|-------------------------------------------|
JSON里面的join_date可以转换成timestamp数据类型而不是上面的STRING吗?
JSON 没有“日期”数据类型,它有 numbers, bool, string。因此,您需要将 dates/timestamps 存储为纪元 seconds/milliseconds 或格式化字符串。
SELECT
try_to_timestamp_ntz('2021-06-18 19:51:19.000') as t
,date_part('epoch_second', t) as epoch_s
,date_part('epoch_millisecond', t) as epoch_ms
,to_timestamp_ntz(epoch_s, 0) as time_from_sec
,to_timestamp_ntz(epoch_ms, 3) as time_from_ms;
给予:
T
EPOCH_S
EPOCH_MS
TIME_FROM_SEC
TIME_FROM_MS
2021-06-18 19:51:19.000
1624045879
1624045879000
2021-06-18 19:51:19.000
2021-06-18 19:51:19.000
啊所以,你使用 JSON 的错误是 VARIANT/json 对象的值是 VARIENT 所以要将它们传递给 TO_TIMESTAMP 之类的东西,他们需要转换为 string
下面是构建、转换和提取值:
Select
user.id
,user.create_date
,OBJECT_CONSTRUCT(
'user_id', user.id,
'join_date', user.create_date
)::variant as json_data
,get(json_data, 'user_id') AS j_user_id_1
,json_data:user_id AS j_user_id_2
,get(json_data, 'join_date') AS j_user_create_date_1
,json_data:join_date AS j_user_create_date_2
,j_user_id_1::number as j_user_id_1_int
,j_user_create_date_2::timestamp as j_user_create_date_2_ts_a
,to_timestamp_ntz(j_user_create_date_2::string) as j_user_create_date_2_ts_b
,try_to_timestamp_ntz(j_user_create_date_2::string) as j_user_create_date_2_ts_c
FROM VALUES
(1, '2021-06-18 19:51:19.000' )
AS user(id, create_date)
;
我正在使用 Snowflake 的 Object_Construct 创建一个包含值的 JSON 数组。
示例:
Select OBJECT_CONSTRUCT(
user_id, user.id::TIMESTAMP,
join_date, user.create_date
)
from user
Select object_construct(*) from above_table
这给我的输出如下
|-------------------------------------------|
| OBJECT_CONSTRUCT(*) |
|-------------------------------------------|
| { |
| "user_id": 1, |
| "join_date": "2021-06-18 19:51:19.000" |
| } |
|-------------------------------------------|
JSON里面的join_date可以转换成timestamp数据类型而不是上面的STRING吗?
JSON 没有“日期”数据类型,它有 numbers, bool, string。因此,您需要将 dates/timestamps 存储为纪元 seconds/milliseconds 或格式化字符串。
SELECT
try_to_timestamp_ntz('2021-06-18 19:51:19.000') as t
,date_part('epoch_second', t) as epoch_s
,date_part('epoch_millisecond', t) as epoch_ms
,to_timestamp_ntz(epoch_s, 0) as time_from_sec
,to_timestamp_ntz(epoch_ms, 3) as time_from_ms;
给予:
T | EPOCH_S | EPOCH_MS | TIME_FROM_SEC | TIME_FROM_MS |
---|---|---|---|---|
2021-06-18 19:51:19.000 | 1624045879 | 1624045879000 | 2021-06-18 19:51:19.000 | 2021-06-18 19:51:19.000 |
啊所以,你使用 JSON 的错误是 VARIANT/json 对象的值是 VARIENT 所以要将它们传递给 TO_TIMESTAMP 之类的东西,他们需要转换为 string
下面是构建、转换和提取值:
Select
user.id
,user.create_date
,OBJECT_CONSTRUCT(
'user_id', user.id,
'join_date', user.create_date
)::variant as json_data
,get(json_data, 'user_id') AS j_user_id_1
,json_data:user_id AS j_user_id_2
,get(json_data, 'join_date') AS j_user_create_date_1
,json_data:join_date AS j_user_create_date_2
,j_user_id_1::number as j_user_id_1_int
,j_user_create_date_2::timestamp as j_user_create_date_2_ts_a
,to_timestamp_ntz(j_user_create_date_2::string) as j_user_create_date_2_ts_b
,try_to_timestamp_ntz(j_user_create_date_2::string) as j_user_create_date_2_ts_c
FROM VALUES
(1, '2021-06-18 19:51:19.000' )
AS user(id, create_date)
;