【问题标题】:choose from a time period oracle sql从时间段中选择oracle sql
【发布时间】:2022-01-16 17:50:40
【问题描述】:

我在 FIRST_DATE 列中有时间戳日期格式,我需要从某个小时中选择时间段,例如。 从 18:00 10.05.21 到 18:00 11.05.2021

问题在于时间戳格式的日期列 - FIRST_DATE: 10/05/2020 0:00:03,000000 TIMESTAMP(6)

所以我尝试使用它:

select
count(*)
from TABLE
where to_char(FIRST_DATE, 'HH24:MI')>='18:00'

所以通过这种方式,我可以按时间限制开始时间,但如果我在其中添加日期,我的条件将停止工作

and to_char(FIRST_DATE, 'DD-MON-YY')>='10-MAY-21'

如何更正我的脚本以选择从 18:00 10.05.21 到 18:00 11.05.2021 的所有内容

【问题讨论】:

    标签: sql oracle oracle-sqldeveloper


    【解决方案1】:

    不要将日期(或时间戳)与字符串进行比较。 '18:00''10-MAY-21'字符串。使用带有适当格式掩码的 TO_TIMESTAMP,例如(第 5 行和第 6 行):

    SQL> with test (first_date) as
      2    (select to_timestamp('10/05/2020 23:00:03,000000', 'dd/mm/yyyy hh24:mi:ss,ff3') from dual)
      3  select *
      4  from test
      5  where first_date between to_timestamp('10/05/2020 18:00:00,000000', 'dd/mm/yyyy hh24:mi:ss,ff3')
      6                       and to_timestamp('11/05/2020 18:00:00,000000', 'dd/mm/yyyy hh24:mi:ss,ff3')
      7  /
    
    FIRST_DATE
    ---------------------------------------------------------------------------
    10.05.20 23:00:03,000000000
    
    SQL>
    

    【讨论】:

    • 是的,问题出在正确的格式掩码中,谢谢
    猜你喜欢
    • 2015-05-14
    • 2017-07-10
    • 2018-06-14
    • 1970-01-01
    • 2015-12-31
    • 2021-10-31
    • 2018-09-01
    • 1970-01-01
    • 2012-05-20
    相关资源
    最近更新 更多