5

我试过但无法得到正确的解决方案。我想要一个列出当年所有周末日期的 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....

4

9 回答 9

6

看看这篇博文。

您的问题已详细解释。

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)
于 2012-09-19T06:34:25.527 回答
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    
于 2012-09-19T06:40:53.763 回答
2

你可以试试这个

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)
于 2012-09-19T06:37:42.727 回答
0

尝试通过执行以下操作找到第一个星期六:

  1. 开始于 2012-01-01
  2. 如果不是星期六,就加一天
  3. 转到 2

然后,在一个临时表中,添加该日期和下一个日期(星期日)。之后,循环以下内容:

  1. 将 7 天和 8 天添加到您找到的最后一个星期六(您会得到下一个星期六和星期日)
  2. 检查他们是否还在2012年
  3. 如果是,请将它们存储在临时表中并转到 1

可能有更优雅的方法,但这是我快速而肮脏的解决方案。由于您没有发布任何您尝试过的代码,我将把实现留给您。

于 2012-09-19T06:31:54.487 回答
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 
于 2012-09-19T06:41:38.107 回答
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或语言设置。

于 2012-09-19T06:48:39.387 回答
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)
于 2012-09-19T06:58:57.633 回答
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'
于 2012-09-19T07:05:34.300 回答
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)
于 2020-12-04T15:58:49.343 回答