【问题标题】:Redshift: Convert text to timestamp [duplicate]Redshift:将文本转换为时间戳[重复]
【发布时间】:2020-05-30 15:57:43
【问题描述】:

我有一个带有 json 值的分数列 -

{"Choices":null,
"timestamp":"1579650266955",
"scaledScore":null}

我正在使用下面的 sql 来检索时间戳值 --

select json_extract_path_text(score, 'timestamp') from schema.table limit 10;

现在我需要将其转换为实际时间戳。我一直在尝试一些事情,但所有事情都失败了

ERROR:  Invalid data
DETAIL:
  -----------------------------------------------
  error:  Invalid data
  code:      8001
  context:   Invalid format or data given: 1579650266955
  query:     2057693
  location:  funcs_timestamp.cpp:261
  process:   query1_120_2057693 [pid=6659]
  -----------------------------------------------

我尝试过的 SQL --

select cast(json_extract_path_text(score, 'timestamp') as timestamp) from schema.table limit 10;

select cast(replace(json_extract_path_text(score, 'timestamp'), 'T','') as timestamp) from schema.table limit 10;

select timestamp 'epoch' + json_extract_path_text(score, 'timestamp') * interval '1 second' from schema.table limit 10;
ERROR:  operator does not exist: text * interval
HINT:  No operator matches the given name and argument type(s). You may need to add explicit type casts.
Time: 451.429 ms

【问题讨论】:

  • 这工作select timestamp 'epoch' + cast(json_extract_path_text(score, 'timestamp') as bigint)/1000 * interval '1 second' from schema.table limit 10;

标签: sql amazon-redshift amazon-redshift-spectrum


【解决方案1】:

以下 SQL 有效 --

select timestamp 'epoch' + cast(json_extract_path_text(score, 'timestamp') as bigint)/1000 * interval '1 second' from schema.table limit 10;

【讨论】:

    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2014-06-26
    • 1970-01-01
    相关资源
    最近更新 更多