【问题标题】:Oracle Date/Time manipulation queryOracle 日期/时间操作查询
【发布时间】:2014-09-12 00:33:22
【问题描述】:

我无法在 oracle SQL 中为我的查询找到解决方案:

这是我需要的查询示例:

Select Result_Date as hour , Type , Count(Id) as Number From Results 
Where Result_Date Between Today'06:00' And today'14:00'
group by result_date , type 

结果日期包含小时,结果日期的格式为时间戳。 我怎样才能完成这个查询而不在 between 语句中输入日期,只有我想搜索今天之间的小时数。

如果您有任何想法,请告诉我,欢迎任何想法。

【问题讨论】:

  • to_char 函数就是你想要的。
  • 我已经尝试过 to_char 并且它没有比较字符串
  • 我已经更正了代码,今天在工作时间之前添加了

标签: sql oracle timestamp


【解决方案1】:

您可以在时间戳上使用trunc() 函数,这样就可以满足您的需求:

Select trunc(Result_Date, 'HH24') as hour , Type , Count(Id) as Number From Results 
Where trunc(Result_Date, 'HH24') Between '06:00' And '14:00' and
      trunc(Result_Date) = trunc(sysdate)
group by trunc(Result_Date, 'HH24'), type ;

注意:

trunc() 日期默认为当天。如果您愿意,可以为此添加特定格式。

【讨论】:

  • 我也只在当前一天需要它,比如在 current_day '06:00' 和 current_day '14:00' 之间
【解决方案2】:

编辑,这对我有用

   Select to_number(extract(hour from Result_Date)) as hour , Type , Count(Id) as Number From Results Where To_Date(Trunc(Result_Date)) = To_Date(Trunc(Current_Timestamp))
        and to_number(extract(hour from Result_Date)) Between 6 and 14     
            group by  to_number(extract(hour from )Result_Date) as hour  , type 

@Gordon Linoff,谢谢你对trunk的建议,它帮助我分配了

【讨论】:

    猜你喜欢
    • 2019-04-27
    • 2021-07-24
    • 2018-05-27
    • 2011-02-24
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多