简体   繁体   English

函数计算正确的星期数

[英]function calculate the right week number of year

I am looking for function that give me the right week-number . 我正在寻找可以给我合适的week-number函数。

The first day of the year should always be on week number 1. The first saturday of a year belongs to the first week. 一年的第一天应始终在第1周。一年的第一个星期六属于第一周。

In another words: here is what i am looking for, but not in PostgreSQL , but in sql-server . 换句话说: 这是我要寻找的东西,但不是在PostgreSQL ,而是在sql-server

I realy want to do that using a function that i will call later. 我真的想使用我稍后会调用的函数来做到这一点。

Thank you in advance for your help. 预先感谢您的帮助。

In SQL Server there's no need for a custom function; 在SQL Server中,不需要自定义函数。 just use the built in DATEPART function with week as the first argument: 只需使用内置的DATEPART函数,并将week作为第一个参数:

-- first day
select datepart(week, '2013-01-01');

Output: 1 输出1

-- first Saturday
select datepart(week, '2013-01-05');

Output: 1 输出1

-- first Sunday
select datepart(week, '2013-01-06');

Output: 2 输出2

-- today
select datepart(week, '2013-04-01');

Output: 14 输出:14

Here's a live demo: http://www.sqlfiddle.com/#!3/d41d8/11802 这是一个现场演示: http : //www.sqlfiddle.com/#!3/ d41d8/ 11802

选择datepart(week,getdate())作为CurrentWeekofYear

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

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