T SQL日期范围之间的第一个星期二

时间:2019-04-05 10:51:00

标签: sql-server tsql date dateadd

使用SQL Server,我需要计算出“存储桶开始”日期和“存储桶结束”日期之间的第一个星期二。但是,我的代码返回了错误的日期。

这是我的代码:

SELECT 
    CONVERT(DATETIME, CONVERT(VARCHAR(10), [BUCKET_START])) AS  Bucket_Start,
    CONVERT(DATETIME, CONVERT(VARCHAR(10), [BUCKET_END])) AS Bucket_End,
    DATEADD(dd, - 6, DATEADD(wk, DATEDIFF(wk, 0, dateadd(dd, 7 - DATEPART(day, CONVERT(DATETIME, CONVERT(VARCHAR(10), [BUCKET_START]))),   
    CONVERT(DATETIME, CONVERT(VARCHAR(10), [BUCKET_END])))), 0)) AS [1st_Tuesday]
FROM 
    [BUCKETS]
WHERE 
    CAT_CODE = 1013
    AND BUCKET_START < CONVERT(NVARCHAR, GETDATE(), 112)
    AND BUCKET_END > CONVERT(NVARCHAR, GETDATE(), 112)

这些是当前结果:

Bucket_Start      Bucket_End      1st_Tuesday
2019-03-31        2019-04-27      2019-03-26

但是,正确的1st_Tuesday是:

Bucket_Start      Bucket_End      1st_Tuesday
2019-03-31        2019-04-27      2019-04-02

为什么我的查询在三月份仍然显示?

感谢您的帮助。

4 个答案:

答案 0 :(得分:0)

如果是星期三,则将6天添加到存储桶中;如果是星期四,则添加5天; ...,如果是星期二,则添加0。

SELECT Bucket_Start, DATEADD(DAY, CASE DATENAME(WEEKDAY, Bucket_Start_Date)
    WHEN 'Wednesday' THEN 6
    WHEN 'Thursday'  THEN 5
    WHEN 'Friday'    THEN 4
    WHEN 'Saturday'  THEN 3
    WHEN 'Sunday'    THEN 2
    WHEN 'Monday'    THEN 1
    WHEN 'Tuesday'   THEN 0
END, Bucket_Start_Date) AS First_Tuesday
FROM Buckets
CROSS APPLY (
    -- convert int 20190301 to date
    SELECT CAST(CAST(Bucket_Start AS VARCHAR(8)) AS DATE) AS Bucket_Start_Date
) AS CA

答案 1 :(得分:0)

我们可以使用case语句来实现。

    SELECT 
CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])) AS Bucket_Start,
CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_END])) AS Bucket_End,
case when     abs((select  DATEparT(weekday  ,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))))) in  (1,2,3)
                  then    DATEadd(weekday,  3 -  abs((select  DATEparT(weekday  ,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))))), CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
                  else   DATEadd(weekday,  3 + ( 7 - abs((select  DATEparT(weekday  ,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))))), CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))  
                  end AS [1st_Tuesday]
FROM [BUCKETS]
WHERE   BUCKET_START < CONVERT(NVARCHAR, GETDATE(), 112)
AND BUCKET_END > CONVERT(NVARCHAR, GETDATE(), 112)

答案 2 :(得分:0)

感谢大家的帮助!我已经遵循以下条件:

SELECT
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) as Bucket_Start_Day, 
CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])) AS Bucket_Start,
CASE
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Sunday' then 
DATEADD(DD,2,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Monday' then 
DATEADD(DD,1,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Tuesday' then 
CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Wednesday' then 
DATEADD(DD,6,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Thursday' then 
DATEADD(DD,5,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Friday' then 
DATEADD(DD,4,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
WHEN
DATENAME(dw,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START]))) = 'Saturday' then 
DATEADD(DD,4,CONVERT(DATETIME, convert(VARCHAR(10), [BUCKET_START])))
END AS '1st_Tuesday'
FROM [BUCKETS]
WHERE CAT_CODE = 1013
AND BUCKET_START < CONVERT(NVARCHAR, GETDATE(), 112)
AND BUCKET_END > CONVERT(NVARCHAR, GETDATE(), 112)

这给了我以下内容:

Bucket_Start_Day    Bucket_Start               1st_Tuesday
Sunday          2019-03-31 00:00:00.000    2019-04-02 00:00:00.000

答案 3 :(得分:0)

这并非您所要的,但我敢肯定您可以使用它来获得所需的结果。

DECLARE  @Day INT = 3;
 --Sunday    1
 --Monday    2
 --Tuesday   3
 --Wednesday 4
 --Thurday   5
 --Friday    6
 --Saturday  7 
 DECLARE  @StartDate DATETIME = GETDATE();

 SELECT  DATEADD(DD, @Day - DATEPART(WEEKDAY, @StartDate) + IIF(@Day - DATEPART(WEEKDAY, @StartDate) >= 0, 0, 7), @StartDate)

如果要下一个不包括今天的内容,请将> =更改为>