Group by weeks in SQL Server
Feb 22, 2021
Hi, If you want to group by the date result of the query. You can use the datepart function in the SQL server but, If the start of week configuration is set for a different day for your country, and let's assume you have some jobs other things you shouldn't change the start day of week configuration (you can check SELECT @@DATEFIRST). I found a method for grouping results by week, without datepart function.
select DATEDIFF(week, ‘startDate’, DATEADD(day, -1, ‘endDate’))
- -1 for the week starts with monday
- -7 for the week starts with Sunday
it calculates differences of these dates and divide by 7 and give the result
if you multiple this result by 7 you will achieve the differences of days