Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Return WeekNo (ISO 8601 standard)

By L Xu, 2005/06/14

In MS SQL, January 1 of any year defines the starting number for the week. The DATEPART(wk, 'Jan 1, xxxx') always return 1. However, a lot of reports Week No is actually based on ISO 8601 standard, that is January 1 of any year can be Week 53/52 or Week 1. (http://www.merlyn.demon.co.uk/weekinfo.htm#IDC)

The Rule is as follows:
The first Week of a Year is Number 01, which is :-
*defined as being the week which contains the first Thursday of the Calendar year; which implies that it is also :-
*the first week which is mostly within the Calendar year,
*the week containing January 4th,
*the week starting with the Monday nearest to January 1st.

The conditions are mutually equivalent. See Markus Kuhn and R.H. van Gent, via datefmts.htm, for example.
The last Week of a Year, Number 52 or 53, therefore is :-
*the week which contains the last Thursday of the Calendar year;
*the last week which is mostly within the Calendar year;
*the week containing December 28th;
*the week ending with the Sunday nearest to December 31st.

Run the scripts below, and test

Declare @tDate Datetime
set @tDate =getdate()
select dbo.udf_DT_ISOWeekNum (@tDate)

Total article views: 214 | Views in the last 30 days: 1
 
Related Articles
FORUM

Temporary Table

Calendar

ARTICLE

Calendar Tables

Learn how to avoid complex date calculations and increase performance using calendar tables in this ...

SCRIPT

Date Calendar

Code to create a handy date calendar cross-reference table with a ton of pre-populated, slice-and-di...

ARTICLE

4-4-5 Calendar Functions, Part 2

Part 2 showing how to get the Period in a 4-4-5 Calendar

ARTICLE

4-4-5 Calendar Functions, Part 1

New author Cliff Corder has a SQL Server function to report on a 4-4-5 Calendar Accounting Year.

Tags
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones