Get yesterday's rows startign from 10AM

落花浮王杯 提交于 2019-12-12 01:39:23

问题


For previous day I used use the below expression .

     DATE_INSERTED >=DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0) 
     AND  DATE_INSERTED < DATEADD(day, DATEDIFF(day,0,GETDATE()),0)

How to get rows from yesterday 10Am to today 10AM


回答1:


-- yesterday at midnight:
DECLARE @yesterday DATETIME = DATEADD(DAY,DATEDIFF(DAY,1,GETDATE()),0);

SELECT
...
WHERE DATE_INSERTED >= DATEADD(HOUR, 10, @yesterday)  -- 10 AM yesterday
  AND DATE_INSERTED <  DATEADD(HOUR, 34, @yesterday); -- 10 AM today



回答2:


Instead of using zeroes, use some date(time)s that have the desirable properties:

DATE_INSERTED >=
    DATEADD(day, DATEDIFF(day,'20010102',GETDATE()),'2001-01-01T10:00:00') 
 AND  DATE_INSERTED <
    DATEADD(day, DATEDIFF(day,'20010102',GETDATE()),'2001-01-02T10:00:00')

I.e. if you add the total number of days that have occurred since 2nd January 2001 onto 10:00am on the 1st January 2001, you'll always obtain a value which is "yesterday at 10am". The second one is almost identical.



来源:https://stackoverflow.com/questions/18105986/get-yesterdays-rows-startign-from-10am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!