0

我有一个使用 Windows 调度程序自动运行的程序。该程序所做的是运行“昨天”结果的查询......然后过滤结果,不包括特定时间范围内的任何内容。我现在遇到的问题是我必须在查询中指定日期......我怎样才能让查询自动排除时间(早上 5:30 到早上 6:15)

SELECT  Store_Id, DM_Corp_Received_Date
FROM    Register_Till_Count_Tb
WHERE   (DM_Corp_Received_Date >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) 
    AND (Register_Transaction_Type = 'sod') 
    AND (DM_Corp_Received_Date < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) 
    AND (DM_Corp_Received_Date NOT BETWEEN CONVERT(datetime, '2012-08-08 05:30:00', 102) AND CONVERT(Datetime, '2012-08-08 06:15:00', 102))
4

1 回答 1

1
DECLARE @Yesterday DATETIME = DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0));
DECLARE @Today DATETIME = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0));

SELECT  Store_Id, DM_Corp_Received_Date
FROM    Register_Till_Count_Tb
WHERE   (DM_Corp_Received_Date >= @Yesterday
    AND (Register_Transaction_Type = 'sod') 
    AND (DM_Corp_Received_Date < @Today
    AND (DM_Corp_Received_Date NOT BETWEEN DATEADD(minute, 5*60+30, @Yesterday) AND   
         DATEADD(minute, 6*60+15, @Yesterday))

请注意,这BETWEEN是包容性的,所以要小心你的界限。

于 2012-08-09T17:22:55.733 回答