我需要从每天17:00到第二天早上08:00每天返回数据。
如何在查询中仅指定要每天返回的数据的时间?
这是我迄今为止的查询:
DECLARE @yesterday SMALLDATETIME;
SET @yesterday = DATEADD(DAY,-1,CONVERT(SMALLDATETIME, CONVERT(DATE, GETDATE())));
SELECT Desired columns
FROM Desired Table
WHERE Start >= DATEADD(HOUR, 17, @yesterday) and Start < DATEADD(HOUR, 24, @yesterday)
这能满足您的需求吗:
-- Today as just a date
declare @today DateTime = convert(date, getdate());
-- Yesterday @ 5pm
declare @start DateTime = dateAdd(hour, -7, @today);
-- Today @ 8am
declare @end DateTime = dateAdd(hour, 8, @today);
SELECT Desired columns
FROM Desired Table
WHERE Start between @start and @end