Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:12:59+00:00 2026-06-11T13:12:59+00:00

I tried but could not get the right solution. I want an SQL query

  • 0

I tried but could not get the right solution. I want an SQL query that lists 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

Its output is 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 beginning – so, the above is missing one weekend, which is 2012-07-01. Also, I want the week numbers to show as 1, 2, 3... instead of 52, 51....

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:13:00+00:00Added an answer on June 11, 2026 at 1:13 pm

    Check out 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 IN ('Saturday','Sunday') -- For Weekend    
    /*
    WHERE DayName LIKE 'Sunday'
    WHERE DayName NOT IN ('Saturday','Sunday') -- For Weekday
    WHERE DayName LIKE 'Monday' -- For Monday
    WHERE DayName LIKE 'Sunday' -- For Sunday
    */
    OPTION (MaxRecursion 370)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried this, but could not get through:- code behind protected HtmlTableRow trComment; protected
I tried following the answer to this question , but could not get xsd.exe
I've tried looking at similar problems, but could not easily find one that helped
Question says it all. I tried browsing through the latest tag but could not
I tried searching in google but could not find any useful answer, if(verifier.length() >
I tried to find the answer to this question but could not, to be
A really simple question but I could not figure it out... I tried to
i have tried looking online but had no luck, How i could delete all
How could I want to add a column if not exist. I tried this
I search everywhere and tried everything I could, but I cannot make my second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.