Gerrit Mantel
Programs SQL_SCALAR_FUNCTION - fun_date2isoweek

Main program

Namefun_date2isoweek
Type_descSQL_SCALAR_FUNCTION
CommentSQLServer User-Defined Function

PURPOSE
Return the ISO weeknumber for a given date.

This function is based on a Excel formula developed by Evert van den Heuvel,
and is published at http://www.cpearson.com/excel/weeknum.htm
=1+INT((A1-DATE(YEAR(A1+4-WEEKDAY(A1+6));1;5)+WEEKDAY(DATE(YEAR(A1+4-WEEKDAY(A1+6));1;3)))/7)
where A1 is the datecell, and ; is the separation character.

PARAMETERS
@dt DATETIME Given date
RETURNS INT ISO weeknumber

EXAMPLES
SELECT dbo.fun_date2isoweek(GETDATE())
43

HISTORY
2004-12-07 - Created function
2014-10-21 - Renamed function from fun_weeknr to fun_date2isoweek
2024-02-09 - Changed history table

TAGS
<program>
<description>Return the ISO weeknumber for a given date</description>
<generic>1</generic>
<author>Gerrit Mantel</author>
<created>2004-12-07</created>
<lastmodified>2024-02-09</lastmodified>
</program>
DescriptionReturn the ISO weeknumber for a given date
Minversion
Generic1
AuthorGerrit Mantel
Created2004-12-07 00:00:00
Lastmodified2024-02-09 00:00:00
Xp_cmdshell0
Ole_automation0
Subprogram_cnt0
Sourcecode