【问题标题】:Redshift: Get hour from Time (HH:MM:SS) columnRedshift:从时间 (HH:MM:SS) 列获取小时
【发布时间】:2019-07-10 12:15:52
【问题描述】:

我有一个时间列,其值采用 HH:MM:SS 格式。如何从该列中仅获取小时?

我尝试过使用 extract(hour) 函数。

select extract(hour from opentime::datetime)
from transaction

我收到一条错误消息,提示 Amazon Invalid operation:Timestamp out of range。我认为这是因为 opentime 列不完全是 DateTime,而只是时间。

我只需要得到小时。

column value --> 09:00:00
expected output --> 09

【问题讨论】:

  • 您可能需要添加一个假日期,例如extract(hour from ('2000-01-01 ' || opentime)::datetime)
  • 嘿约翰,这个解决方法奏效了。但是我有很多查询需要应用它,这使得整体查询性能变慢。如果您有任何其他想法,请分享并感谢您的解决方案。
  • 如果列是字符串,则使用:LEFT(opentime, 2)::integer
  • 嗨,John,感谢这个解决方法在查询时提供了更好的性能。

标签: amazon-redshift


【解决方案1】:

如果输入列是字符串,则提取小时:

LEFT(opentime, 2)::integer

【讨论】:

    【解决方案2】:

    您可以对TIME 列执行SUBSTRING 操作

    select '13:21:23'::TIME as t, substring(t, 1, 2);
    

    返回

    t           hour
    13:21:23    13
    

    【讨论】:

    • 嘿botchniaque,执行'13:21:23'::time 时出现此错误“无效操作:数据类型未实现”
    • 奇怪 - 你在哪个 Redshift 版本?我对 1.0.8205 执行了上述查询
    猜你喜欢
    • 2020-11-30
    • 2017-06-27
    • 1970-01-01
    • 2019-01-18
    • 2018-04-17
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多