从日期获取年份中的周数 [英] Get week number in year from date

查看:31
本文介绍了从日期获取年份中的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个查询,它将返回给定日期的周数.

I am looking for a query which will return the week number from given date.

我已经尝试过的是:

select datepart(wk, '2017-02-01')

但这会返回 5 而不是 6.(2 月 1 日是一年中的第 6 周).

but this returns 5 instead of 6. (february 1st is the 6th week of the year).

(带有红色的周数)

推荐答案

您可能需要使用 设置日期优先:

set datefirst 1;
select datepart(week, '2017-02-01');

返回6

根据默认语言,您的 datefirst 可能设置为 7.

Depending on the default language, your datefirst might be set to 7.

set datefirst 7;
select datepart(week, '2017-02-01');

返回5

rextester 演示:http://rextester.com/KEPB52852

rextester demo: http://rextester.com/KEPB52852

这篇关于从日期获取年份中的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆