如何在SQL中获取当年的所有周末日期?

时间:2012-09-19 06:26:51

标签: sql sql-server-2008

我尝试但无法获得正确的解决方案。我想要一个SQL查询,列出当年的所有周末日期。

我尝试了这个SQL查询:

WITH hier(num, lvl) AS (
    SELECT  0, 1
            UNION ALL
    SELECT  100, 1
            UNION ALL 
    SELECT  num + 1, lvl + 1
    FROM    hier
    WHERE   lvl < 100 
)
SELECT lvl [Week], 
convert(date,DATEADD(dw, -DATEPART(dw, DATEADD(wk,DATEDIFF(wk,0,'12/31/'+convert(nvarchar,YEAR(getdate()))), 0)+6 ),
DATEADD(wk, DATEDIFF(wk,0,'12/31/'+convert(nvarchar,YEAR(getdate()))), 0)+6 ) - num  * 7,101) [End Date]
FROM    hier a
where   num < 52
ORDER BY [End Date] asc

它的输出是这样的:

Week  End date
52  2012-01-14
51  2012-01-21
50  2012-01-28
49  2012-02-04

我希望日期从头开始 - 所以,上面缺少一个周末,2012-07-01。另外,我希望周数显示为1, 2, 3...,而不是52, 51...

9 个答案:

答案 0 :(得分:6)

查看this博文。

详细解释了您的问题。

DECLARE @Year AS INT,
@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME
-- You can change @year to any year you desire
SELECT @year = 2010
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)
-- Creating Query to Prepare Year Data
;WITH cte AS (
SELECT 1 AS DayID,
@FirstDateOfYear AS FromDate,
DATENAME(dw, @FirstDateOfYear) AS Dayname
UNION ALL
SELECT cte.DayID + 1 AS DayID,
DATEADD(d, 1 ,cte.FromDate),
DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
FROM cte
WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear
)
SELECT FromDate AS Date, Dayname
FROM CTE
WHERE DayName LIKE 'Sunday'
/*
WHERE DayName IN ('Saturday,Sunday') -- For Weekend
WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday
WHERE DayName LIKE 'Monday' -- For Monday
WHERE DayName LIKE 'Sunday' -- For Sunday
*/
OPTION (MaxRecursion 370)

答案 1 :(得分:4)

这会有帮助吗

DECLARE @startDate DATETIME, @endDate DATETIME
SELECT @startDate = '2012-01-01', @endDate = '2012-12-31'
;WITH Calender AS (
    SELECT @startDate AS dt
    UNION ALL
    SELECT dt + 1 FROM Calender
    WHERE dt + 1 <= @endDate
)
SELECT 
dt
,NameMonth = DATENAME(Month, dt)
,NameDay = DATENAME (Weekday,dt)
,WeekofYr = DATEPART(WEEK, dt)  FROM Calender
WHERE DATENAME (Weekday,dt) IN ('Sunday')
Option(MaxRecursion 0)

<强>结果(部分)

dt                      NameMonth   NameDay WeekofYr
2012-01-01 00:00:00.000 January     Sunday  1
2012-01-08 00:00:00.000 January     Sunday  2
...............................................
...............................................
2012-12-30 00:00:00.000 December    Sunday  53    

答案 2 :(得分:0)

尝试通过这样做找到第一个星期六:

  1. 从2012-01-01开始
  2. 如果不是星期六,请添加一天
  3. 转到2
  4. 然后,在临时表中添加该日期和下一个日期(星期日)。 之后,循环以下内容:

    1. 在您找到的最后一个星期六(您将在下周六和周日获得)中添加7天和8天
    2. 检查它们是否仍在2012年
    3. 如果是,请将它们存储在临时表中并转到1
    4. 可能有更优雅的方式,但这是我的快速和肮脏的解决由于您没有发布任何已尝试过的代码,我会将实现留给您。

答案 3 :(得分:0)

你可以试试这个

DECLARE @FirstDateOfYear DATETIME
SET @FirstDateOfYear = ’2010-01-01′
SELECT DISTINCT DATEADD(d, number, @FirstDateOfYear),
CASE DATEPART(dw, DATEADD(d, number, @FirstDateOfYear))
WHEN 7 THEN ‘Saturday’
WHEN 1 THEN ‘Sunday’
ELSE ‘Work Day’
END
FROM master..spt_values
WHERE number BETWEEN 0 AND 364
AND (DATEPART(dw, DATEADD(d, number, @FirstDateOfYear)) = 1 OR DATEPART(dw, DATEADD(d, number, @FirstDateOfYear)) = 7)
ORDER BY DATEADD(d, number, @FirstDateOfYear)

答案 4 :(得分:0)

这也有效

declare @dat datetime, @add int

set @dat = '20120101'
set @add = datepart(w,@dat)

set @add = 5 - @add -- friday

set @dat = dateadd(d,@add,@dat)

while @dat <= '20121231'
begin
    print @dat
    set @dat = dateadd(d,7,@dat)
end 

答案 5 :(得分:0)

;with AllDaysOfYear (Day) as (
    select DATEADD(year,DATEDIFF(year,0,CURRENT_TIMESTAMP),0) --Jan 1st
    union all
    select DATEADD(day,1,Day) from AllDaysOfYear
    where DATEPART(year,DATEADD(day,1,Day)) = DATEPART(year,CURRENT_TIMESTAMP)
)
select
    ROW_NUMBER() OVER (ORDER BY Day) as WeekNo,
    Day
from
    AllDaysOfYear
where
    DATEPART(weekday,Day) = DATEPART(weekday,'20120714')
option (maxrecursion 0)

首先,生成一组当年的所有日期(AllDaysInYear)。然后,选择那些weekday是星期六的人。我使用过的值('20120714')并不是非常重要 - 它必须在任何一年的任何星期六。我只是用它来避免需要特定的DATEFIRST或语言设置。

答案 6 :(得分:0)

此查询显示如何在第一部分中获取今年的第一天和第二年的第一天。计算明年的第一天,以便不进行和比较年份。

;WITH cte(TheDate,NextYear) AS
(
  SELECT CAST(CONVERT(CHAR(4),GETDATE(),112)+'0101' AS DATETIME),
         CAST(YEAR(GETDATE())*10000+10101 AS CHAR(8))
   UNION ALL
  SELECT DateAdd(d,1,TheDate),NextYear
    FROM cte
   WHERE DateAdd(d,1,TheDate)<NextYear
)
  SELECT Week = DatePart(wk,TheDate),
         TheDate
    FROM cte
   WHERE DateName(dw,TheDate) in ('Saturday')
ORDER BY TheDate
OPTION (MAXRECURSION 366)

答案 7 :(得分:0)

with t as 
(
select 1 b
union all
select 1 b
union all
select 1 b
union all
select 1 b
union all
select 1 b
union all
select 1 b
union all
select 1 b
union all
select 1 b
) 
select * from 
(
select 
current_timestamp
-datepart(dy,current_timestamp)
+row_number() over (order by t.b) d
from t, t t1, t t2 
) tmp 
where datepart(yyyy,d)=datepart(yyyy,current_timestamp)
      and
      DATENAME(dw,d)='sunday'

答案 8 :(得分:0)

DECLARE @Year AS INT

SELECT @Year = 2020

;WITH weekends AS (
    SELECT DATEFROMPARTS(@Year, 1, 1) AS dt
          UNION ALL
    SELECT DATEADD(DAY, 1, dt) 
      FROM weekends
     WHERE dt < DATEFROMPARTS(@Year, 12, 31)
)
SELECT dt, DATENAME(MONTH, dt), DATENAME(DW, dt)
FROM weekends
WHERE DATEPART(DW, dt) IN (1, 7)
OPTION(MaxRecursion 366)