如何为替代工作星期六创建存储过程?

时间:2015-08-12 08:19:45

标签: sql-server date stored-procedures

我想创建一个用于显示工作星期六的存储过程。

例如:

上周六工作08/08/2015(年/月/日),然后下周六工作应为22/08/2015(年/月/日)

应该在星期六(15/08/2015)之间省略。同样,我想展示特定年份

2 个答案:

答案 0 :(得分:0)

尝试这样的事情

DECLARE @start_sat_day DATE = Dateadd(d, -( Datepart(dw, Getdate()) % 7 ), Getdate());

WITH cte
     AS (SELECT @start_sat_day AS sat_days
         UNION ALL
         SELECT Dateadd(dd, 14, sat_days)
         FROM   cte
         WHERE  Year(sat_days) = Year(@start_sat_day))
SELECT sat_days
FROM   cte
WHERE  Year(sat_days) = Year(@start_sat_day) 

结果:

sat_days
---------
2015-08-08
2015-08-22
2015-09-05
2015-09-19
2015-10-03
2015-10-17
2015-10-31
2015-11-14
2015-11-28
2015-12-12
2015-12-26

答案 1 :(得分:0)

试试这个。假设是,2015年1月3日是第一个工作周六。

DECLARE @firstsaturday date = '2015-01-03'

;WITH CTE AS
(
SELECT @firstsaturday AS StartDate
UNION ALL
SELECT DATEADD(WK,2,StartDate)
FROM CTE
WHERE YEAR(StartDate) = '2015' 
)
SELECT * FROM CTE
WHERE YEAR(StartDate) = '2015'