Tony Montana
Tony Montana

Reputation: 39

Find Week Number in SQL

I am creating a forecast of orders for the year 2021 and breaking it out by week.

I am using

SELECT DATEPART(WK,'2021-01-01')

to find my weeks however i am running into an issue where 2021-01-01 through 2021-01-03 actually fall into week 53 of year 2021. is there a way i can tell sql to mark those dates as week 53 and start on the 4th as week 1?

Upvotes: 0

Views: 346

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269603

I think you are looking for the "iso_week" option on datepart():

select datepart(iso_week, '2021-01-01')

Getting the ISO year, unfortunately, is trickier, but that is not your question.

Upvotes: 3

Related Questions