Oracle日期/时间操作查询

时间:2014-07-21 13:22:24

标签: sql oracle timestamp

Hello Ladies and Gents,

我在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 

结果日期包含小时,结果日期的格式为时间戳。 如何在不在两个语句之间输入日期的情况下完成此查询,仅在我今天要搜索的时间之间。

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

2 个答案:

答案 0 :(得分:2)

您可以在时间戳上使用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()日期默认为当天。如果您愿意,可以为此添加特定格式。

答案 1 :(得分:0)

编辑,这对我有用

   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的建议,它帮我分配了