Find records produced in the last hour

后端 未结 4 2102
忘了有多久
忘了有多久 2021-02-20 00:59

I have a smalldatetime field named myTime recording when the record was created. I need the syntax that selects records created within the last hour.

4条回答
  •  误落风尘
    2021-02-20 01:19

    If you want whole hours use the following...

    --This Hour
    SELECT  *
    FROM    Whatever
    WHERE   myTime >= dateadd(hour, datediff(hour, 0, GETDATE()), 0)
    
    --Last Hour
    SELECT  *
    FROM    Whatever
    WHERE   myTime  < dateadd(hour, datediff(hour, 0, GETDATE()), 0) AND  myTime >= dateadd(hour, datediff(hour, 0,  DATEADD(HOUR, -1, GETDATE())), 0)
    
    --Hour before last
    SELECT  *
    FROM    Whatever
    WHERE   myTime < dateadd(hour, datediff(hour, 0,  DATEADD(HOUR, -1, GETDATE())), 0) AND  myTime >= dateadd(hour, datediff(hour, 0, DATEADD(HOUR, -2, GETDATE())), 0)
    

提交回复
热议问题