【问题标题】:how to match db column data type time without time zone with time string '03:15:00' in postgresql如何在postgresql中将没有时区的db列数据类型时间与时间字符串'03:15:00'匹配
【发布时间】:2015-07-15 08:32:15
【问题描述】:

我在 postgresql 中有数据类型为 time 而没有时区的列。所以我想将此列中的时间存储与字符串“03:15:00”匹配 我已经写完了。查询

 select * from table1 
 where to_timestamp(timecol)::timestamp without time zone
       = to_timestamp('03:15:00')::timestamp;

这里数据类型为time的列名是'timecol'

谁能告诉我如何匹配上述字段。

【问题讨论】:

  • 改写您的问题:我如何找到timecol 中时间部分为'03:15:00' 的所有行,完全忽略日期部分

标签: postgresql


【解决方案1】:

重新表述您的问题:

如何找到timecol 中时间部分为'03:15:00' 的所有行,完全忽略日期部分

为此,您可以只与TIME 值进行比较,因为当您将TIMETIMESTAMP 进行比较时,TIME 会自动扩展为与TIMESTAMP 具有相同的日期。

例如

select * from table1
where timecol = CAST('03:15:00' AS time)

在更复杂的情况下,您将使用date_trunc 和/或extract 函数。

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 2017-08-20
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    相关资源
    最近更新 更多