SQL Server: Week start from Thrusday

Punnet Patel 21 Reputation points
2023-09-14T03:57:24.4733333+00:00

I want to created a week column as per attached file.

week.PNG

My Week start from Friday-Thursday

I have tried the query. But it gives week1 for dates 1-7 and week2 for dates 8-14.

We want week 1 for 8-14, week 2 for 15-21 and so on

create table #tblUser (createdate date)

--truncate table #tblUser

insert into #tblUser VALUES ('2023-01-01')

insert into #tblUser VALUES ('2023-01-02')

insert into #tblUser VALUES ('2023-01-03')

insert into #tblUser VALUES ('2023-01-04')

insert into #tblUser VALUES ('2023-01-05')

insert into #tblUser VALUES ('2023-01-06')

insert into #tblUser VALUES ('2023-01-07')

insert into #tblUser VALUES ('2023-01-08')

insert into #tblUser VALUES ('2023-01-09')

insert into #tblUser VALUES ('2023-01-10')

insert into #tblUser VALUES ('2023-01-11')

insert into #tblUser VALUES ('2023-01-12')

insert into #tblUser VALUES ('2023-01-13')

insert into #tblUser VALUES ('2023-01-14')

insert into #tblUser VALUES ('2023-01-15')

insert into #tblUser VALUES ('2023-01-16')

insert into #tblUser VALUES ('2023-01-17')

insert into #tblUser VALUES ('2023-01-18')

insert into #tblUser VALUES ('2023-01-19')

insert into #tblUser VALUES ('2023-01-20')

insert into #tblUser VALUES ('2023-01-21')

insert into #tblUser VALUES ('2023-01-22')

insert into #tblUser VALUES ('2023-01-23')

insert into #tblUser VALUES ('2023-01-24')

insert into #tblUser VALUES ('2023-01-25')

insert into #tblUser VALUES ('2023-01-26')

insert into #tblUser VALUES ('2023-01-27')

insert into #tblUser VALUES ('2023-01-28')

insert into #tblUser VALUES ('2023-01-29')

insert into #tblUser VALUES ('2023-01-30')

insert into #tblUser VALUES ('2023-01-31')

insert into #tblUser VALUES ('2023-02-01')

insert into #tblUser VALUES ('2023-02-02')

insert into #tblUser VALUES ('2023-02-03')

insert into #tblUser VALUES ('2023-02-04')

insert into #tblUser VALUES ('2023-02-05')

insert into #tblUser VALUES ('2023-02-06')

insert into #tblUser VALUES ('2023-02-07')

insert into #tblUser VALUES ('2023-02-08')

insert into #tblUser VALUES ('2023-02-09')

insert into #tblUser VALUES ('2023-02-10')

insert into #tblUser VALUES ('2023-02-11')

--select * from #tbluser

SET DATEFIRST 6

SELECT DISTINCT FROM

DATEPART (WEEK, createDate) #tbluser

WHERE createDate

'2023-01-01' AND createDate < '2023-02-11'

ORDER BY DATEPART (WEEK, createDate)

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Olaf Helper 46,541 Reputation points
    2023-09-14T04:51:41.74+00:00

    Create a calendar table with week number as you have define for your business logic.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.