How to get all weekend this year in SQL?

I tried, but could not find the right solution. I want the SQL query to indicate all the weekend dates of the current year.

I tried this SQL query:

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

His output looks like this:

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

I want the dates to start from the very beginning - so, in the text above there is no one weekend that 2012-07-01. In addition, I want the week numbers to appear as 1, 2, 3...instead 52, 51....

+5
source share
8 answers

Bookmark this blog post.

Your question is explained in detail.

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)
+5
source

Will it help

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)

Result (Partial)

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    
+3
source

, :

  • 2012-01-01
  • ,
  • 2

(). :

  • 7 8 , ( ).
  • , 2012 .
  • , temp goto 1

, . - , , .

0

DECLARE @FirstDateOfYear DATETIME
SET @FirstDateOfYear = ’2010-01-01SELECT DISTINCT DATEADD(d, number, @FirstDateOfYear),
CASE DATEPART(dw, DATEADD(d, number, @FirstDateOfYear))
WHEN 7 THEN ‘Saturday’
WHEN 1 THEN ‘Sunday’
ELSEWork DayEND
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)
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 
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 .

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)
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'
0

All Articles