我希望在指定日期之前的星期二(或任何特定的一周)。以下是星期二的示例输入和预期输出:
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/???
使用T-SQL获取上周二(或任何一周)的最合适方式是什么?
答案 0 :(得分:6)
我希望这会对你有所帮助
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将在下面的合成中
Saturday
Tuesday 10/06/2015
答案 1 :(得分:0)
您可以使用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
根据@jpw评论,你必须在下面将DATEFIRST设置为7(默认值):
SET DATEFIRST 7
答案 2 :(得分: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
答案 3 :(得分:0)
最有效的方法是使用DATEADD和DATEDIFF:
这是您需要的所有代码,它也适用于其他工作日
DECLARE @daystoadd int = 1 -- tuesday
SELECT DATEADD(week, datediff(d, @daystoadd, testdate) / 7, @daystoadd)
FROM #temp
答案 4 :(得分:0)
回答一些细节是否仍然有帮助:
declare @given_date datetime = '2015-10-15 00:30'
declare @required_weekday int = 1 -- tuesday
select dateadd(week, datediff(week, 0, @given_date), @required_weekday)
说明:
datediff(week, 0, @given_date)
:返回在 1900-01-01 和给定日期 dateadd(week, number_of_weeks, @required_weekday)
:将计算出的 number_of_weeks 和给定的 required_weekday 添加到初始日历日期 1900-01-01