获取指定日期的上周二(或任何特定日期)

时间:2022-10-21 10:29:14

I want to get previous Tuesday (or any given day of week) for specified date. Here is the sample input and expected output for Tuesday:

我希望在指定日期之前的星期二(或任何特定日期)获得。以下是周二的示例输入和预期输出:

CREATE TABLE #temp(testdate DATETIME);
INSERT INTO  #temp(testdate) VALUES
    ('2015-10-06 01:15'), -- Tue -> Tue 2015-10-06 00:00
    ('2015-10-07 04:30'), -- Wed -> Tue 2015-10-06 00:00
    ('2015-10-08 00:30'), -- Thu -> Tue 2015-10-06 00:00
    ('2015-10-09 21:00'), -- Fri -> Tue 2015-10-06 00:00
    ('2015-10-10 19:00'), -- Sat -> Tue 2015-10-06 00:00
    ('2015-10-11 01:15'), -- Sun -> Tue 2015-10-06 00:00
    ('2015-10-12 13:00'), -- Mon -> Tue 2015-10-06 00:00

    ('2015-10-13 18:45'), -- Tue -> Tue 2015-10-13 00:00
    ('2015-10-14 12:15'), -- Wed -> Tue 2015-10-13 00:00
    ('2015-10-15 10:45'), -- Thu -> Tue 2015-10-13 00:00
    ('2015-10-16 04:30'), -- Fri -> Tue 2015-10-13 00:00
    ('2015-10-17 12:15'), -- Sat -> Tue 2015-10-13 00:00
    ('2015-10-18 00:30'), -- Sun -> Tue 2015-10-13 00:00
    ('2015-10-19 10:45'), -- Mon -> Tue 2015-10-13 00:00

    ('2015-10-20 01:15'), -- Tue -> Tue 2015-10-20 00:00
    ('2015-10-21 23:45'), -- Wed -> Tue 2015-10-20 00:00
    ('2015-10-22 21:00'), -- Thu -> Tue 2015-10-20 00:00
    ('2015-10-23 18:45'), -- Fri -> Tue 2015-10-20 00:00
    ('2015-10-24 06:45'), -- Sat -> Tue 2015-10-20 00:00
    ('2015-10-25 06:45'), -- Sun -> Tue 2015-10-20 00:00
    ('2015-10-26 04:30'); -- Mon -> Tue 2015-10-20 00:00

DECLARE @clampday AS INT = 3; -- Tuesday
SELECT -- DATEADD/DATEPART/@clampday/???

What is the most appropriate way to get the previous Tuesday (or any day of week) using T-SQL?

使用T-SQL获取上周二(或任何一周)的最合适方法是什么?

4 个解决方案

#1


6  

i hope this will help you

我希望这能帮到您

SELECT DATEADD(day,- (DATEPART(dw, testdate) + @@DATEFIRST - 3) % 7,testdate) AS Saturday  
from #temp

OR

要么

SELECT DATENAME(weekday,DATEADD(day,- (DATEPART(dw, testdate) + @@DATEFIRST - 3) % 7,testdate)) +'  '+ 
        CONVERT(nvarchar,DATEADD(day,- (DATEPART(dw, testdate) + @@DATEFIRST - 3) % 7,testdate),101) AS Saturday
from @temp

OUTPUT would be in below formate

输出将在下面的甲酸盐中

Saturday
Tuesday  10/06/2015

Remark: the whole query is just combination and calculation of DATEFIRST, DATEPART and DATEADD to manipulate a time

备注:整个查询只是组合和计算DATEFIRST,DATEPART和DATEADD来操纵时间

#2


0  

You can use get number of week by using DATEPART and then use CASE statement in following:

您可以使用DATEPART使用获取周数,然后使用以下CASE语句:

SELECT
    testdate,
    CASE DATEPART(dw,testdate)  WHEN 1 THEN DATEADD(dd,-5,testdate) 
                                WHEN 2 THEN DATEADD(dd,-6,testdate) 
                                WHEN 3 THEN DATEADD(dd, 0,testdate) 
                                WHEN 4 THEN DATEADD(dd,-1,testdate) 
                                WHEN 5 THEN DATEADD(dd,-2,testdate) 
                                WHEN 6 THEN DATEADD(dd,-3,testdate) 
                                WHEN 7 THEN DATEADD(dd,-4,testdate)                                     
    END
FROM #temp

Accoring to @jpw comment, you have to set DATEFIRST to 7 (default) in following:

根据@jpw注释,您必须在以下内容中将DATEFIRST设置为7(默认值):

SET DATEFIRST 7

#3


0  

The most effective way is using DATEADD and DATEDIFF:

最有效的方法是使用DATEADD和DATEDIFF:

This is all the code you need, it also works for other weekdays

这是您需要的所有代码,它也适用于其他工作日

DECLARE @daystoadd int = 1 -- tuesday


SELECT DATEADD(week, datediff(d, @daystoadd, testdate) / 7, @daystoadd)
FROM #temp

#4


0  

CREATE PROC FIND_TUESDAY_DATE
(
 @MYDATE DATE
)
AS 
BEGIN
    SELECT CASE 
            WHEN DATENAME(DW,CAST(DATEADD(DAY,0,@MYDATE) AS DATE)) = 'Tuesday' OR DATENAME(DW,CAST(DATEADD(DAY,-7,@MYDATE) AS DATE))  = 'Tuesday' 
            THEN CAST(DATEADD(DAY,-7,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-1,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-1,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-2,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-2,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-3,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-3,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-4,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-4,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-5,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-5,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-6,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-6,@MYDATE) AS DATE)
        END
END
GO

#1


6  

i hope this will help you

我希望这能帮到您

SELECT DATEADD(day,- (DATEPART(dw, testdate) + @@DATEFIRST - 3) % 7,testdate) AS Saturday  
from #temp

OR

要么

SELECT DATENAME(weekday,DATEADD(day,- (DATEPART(dw, testdate) + @@DATEFIRST - 3) % 7,testdate)) +'  '+ 
        CONVERT(nvarchar,DATEADD(day,- (DATEPART(dw, testdate) + @@DATEFIRST - 3) % 7,testdate),101) AS Saturday
from @temp

OUTPUT would be in below formate

输出将在下面的甲酸盐中

Saturday
Tuesday  10/06/2015

Remark: the whole query is just combination and calculation of DATEFIRST, DATEPART and DATEADD to manipulate a time

备注:整个查询只是组合和计算DATEFIRST,DATEPART和DATEADD来操纵时间

#2


0  

You can use get number of week by using DATEPART and then use CASE statement in following:

您可以使用DATEPART使用获取周数,然后使用以下CASE语句:

SELECT
    testdate,
    CASE DATEPART(dw,testdate)  WHEN 1 THEN DATEADD(dd,-5,testdate) 
                                WHEN 2 THEN DATEADD(dd,-6,testdate) 
                                WHEN 3 THEN DATEADD(dd, 0,testdate) 
                                WHEN 4 THEN DATEADD(dd,-1,testdate) 
                                WHEN 5 THEN DATEADD(dd,-2,testdate) 
                                WHEN 6 THEN DATEADD(dd,-3,testdate) 
                                WHEN 7 THEN DATEADD(dd,-4,testdate)                                     
    END
FROM #temp

Accoring to @jpw comment, you have to set DATEFIRST to 7 (default) in following:

根据@jpw注释,您必须在以下内容中将DATEFIRST设置为7(默认值):

SET DATEFIRST 7

#3


0  

The most effective way is using DATEADD and DATEDIFF:

最有效的方法是使用DATEADD和DATEDIFF:

This is all the code you need, it also works for other weekdays

这是您需要的所有代码,它也适用于其他工作日

DECLARE @daystoadd int = 1 -- tuesday


SELECT DATEADD(week, datediff(d, @daystoadd, testdate) / 7, @daystoadd)
FROM #temp

#4


0  

CREATE PROC FIND_TUESDAY_DATE
(
 @MYDATE DATE
)
AS 
BEGIN
    SELECT CASE 
            WHEN DATENAME(DW,CAST(DATEADD(DAY,0,@MYDATE) AS DATE)) = 'Tuesday' OR DATENAME(DW,CAST(DATEADD(DAY,-7,@MYDATE) AS DATE))  = 'Tuesday' 
            THEN CAST(DATEADD(DAY,-7,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-1,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-1,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-2,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-2,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-3,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-3,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-4,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-4,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-5,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-5,@MYDATE) AS DATE)
            WHEN DATENAME(DW,CAST(DATEADD(DAY,-6,@MYDATE) AS DATE) ) = 'Tuesday' THEN CAST(DATEADD(DAY,-6,@MYDATE) AS DATE)
        END
END
GO