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 7959331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:33:24+00:00 2026-06-04T04:33:24+00:00

Is there is a way to group dates by week of month in SQL

  • 0

Is there is a way to group dates by week of month in SQL Server?

For example

Week 2: 05/07/2012 - 05/13/2012
Week 3: 05/14/2012 - 05/20/2012

but with Sql server statement

I tried

SELECT SOMETHING, 
     datediff(wk, convert(varchar(6), getdate(), 112) + '01', getdate()) + 1 AS TIME_
FROM STATISTICS_
GROUP BY something, TIME_
ORDER BY TIME_

but it returns the week number of month. (means 3)

How to get the pair of days for current week ?

For example, now we are in third (3rd) week and I want to show 05/14/2012 - 05/20/2012

I solved somehow:

SELECT DATEADD(ww, DATEDIFF(ww,0,<my_column_name>), 0)

select DATEADD(ww, DATEDIFF(ww,0,<my_column_name>), 0)+6

Then I will get two days and I will concatenate them later.

  • 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-04T04:33:24+00:00Added an answer on June 4, 2026 at 4:33 am

    All right, bear with me here. We’re going to build a temporary calendar table that represents this month, including the days from before and after the month that fall into your definition of a week (Monday – Sunday). I do this in a lot of steps to try to make the process clear, but I probably haven’t excelled at that in this case.

    We can then generate the ranges for the different weeks, and you can join against your other tables using that.

    SET DATEFIRST 7;
    SET NOCOUNT ON;
    
    DECLARE @today SMALLDATETIME, @fd SMALLDATETIME, @rc INT;
    
    SELECT @today = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0), -- today
        @fd = DATEADD(DAY, 1-DAY(@today), @today), -- first day of this month
        @rc = DATEPART(DAY, DATEADD(DAY, -1, DATEADD(MONTH, 1, @fd)));-- days in month
    
    DECLARE @thismonth TABLE (
        [date]       SMALLDATETIME,
        [weekday]    TINYINT,
        [weeknumber] TINYINT 
    );
    
    ;WITH n(d) AS (
        SELECT TOP (@rc+12) DATEADD(DAY, ROW_NUMBER() OVER 
        (ORDER BY [object_id]) - 7, @fd) FROM sys.all_objects
    )
    INSERT @thismonth([date], [weekday]) SELECT d, DATEPART(WEEKDAY, d) FROM n;
    
    DELETE @thismonth WHERE [date] < (SELECT MIN([date]) FROM @thismonth WHERE [weekday] = 2)
                         OR [date] > (SELECT MAX([date]) FROM @thismonth WHERE [weekday] = 1);
    
    ;WITH x AS ( SELECT [date], weeknumber, rn = ((ROW_NUMBER() OVER 
       (ORDER BY [date])-1) / 7) + 1 FROM @thismonth ) UPDATE x SET weeknumber = rn;
    

    — now, the final query given all that (I’ve only broken this up to get rid of the vertical scrollbars):

    ;WITH ranges(w,s,e) AS (
        SELECT weeknumber, MIN([date]), MAX([date]) FROM @thismonth GROUP BY weeknumber
    )
    SELECT [week] = CONVERT(CHAR(10), r.s, 120) + ' - ' + CONVERT(CHAR(10), r.e, 120)
    
     --, SOMETHING , other columns from STATISTICS_?
    
     FROM ranges AS r
    
     -- LEFT OUTER JOIN dbo.STATISTICS_ AS s
     -- ON s.TIME_ >= r.s AND s.TIME_ < DATEADD(DAY, 1, r.e) 
    
     -- comment this out if you want all the weeks from this month: 
     WHERE w = (SELECT weeknumber FROM @thismonth WHERE [date] = @today)
    
     GROUP BY r.s, r.e --, SOMETHING
     ORDER BY [week];
    

    Results with WHERE clause:

    week
    -----------------------
    2012-05-14 - 2012-05-20
    

    Results without WHERE clause:

    week
    -----------------------
    2012-04-30 - 2012-05-06
    2012-05-07 - 2012-05-13
    2012-05-14 - 2012-05-20
    2012-05-21 - 2012-05-27
    2012-05-28 - 2012-06-03
    

    Note that I chose YYYY-MM-DD on purpose. You should avoid regional formatting like M/D/Y especially for input but also for display. No matter how targeted you think your audience is, you’re always going to have someone who thinks 05/07/2012 is July 5th, not May 7th. With YYYY-MM-DD there is no ambiguity whatsoever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to GROUP BY using one column, but a set of
Is there any way to make the option group selectable? <select> <optgroup value=0 label=Parent
Is there a sensible way to group Long UTC dates by Day? I would
Is there any way to group the models in django admin interface? I currently
Is there a way to group a bunch of DLL's and still use them
Is there a way to assign or define group within html markup?
Is there a way to get the name of a captured group in C#?
Is there a way to include a LIKE expression in a GROUP BY query?
I have group of stored procedures with names like 'somename_%'. Are there any way
My Facebook group is about my iPhone software. Is there a way to have

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.