简体   繁体   English

如何从SQL Server中的日期获取星期数?

[英]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 注意:2017年12月29日

The first day of the week is based on the language settings of the server. 一周中的第一天基于服务器的语言设置。 The default setting is US English 7 (Sunday). 默认设置为美国英语7(星期日)。 But you can easily change it by using DATEFIRST . 但是您可以使用DATEFIRST轻松更改它。 Put this line at the top of your query: 将此行放在查询的顶部:

SET DATEFIRST 5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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