简体   繁体   中英

How to get week number from date in SQL Server?

How to get week number from date, if first date is Friday (interval week is Friday - Thursday)?

My query:

declare @week as int
set @week='04'

SELECT datepart(WEEK,DATEADD(DAY,-5,Day))  Week,DAY

FROM [MyTable]
where datepart(WEEK,DATEADD(DAY,-5,Day)) =@week

but the result is wrong.

how to get result like this:

在此处输入图片说明

Note: 29 Dec is in 2017

The first day of the week is based on the language settings of the server. The default setting is US English 7 (Sunday). But you can easily change it by using DATEFIRST . Put this line at the top of your query:

SET DATEFIRST 5;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM