Isoweek in SQL Server 2005

后端 未结 4 1570
天涯浪人
天涯浪人 2020-11-29 07:53

In SQL Server 2008 the isoweek can be found with this:

SELECT datepart(iso_week, getdate())

Before SQL Server 2008 there were no built-in f

4条回答
  •  隐瞒了意图╮
    2020-11-29 08:04

    I needed something similar for PowerQuery & PowerBI and based on t-clausen.dk's response I was able to make this equation. It works the same as his but uses PowerQuery syntax. Also the base date for the DATEDIFF of 0 was 1/1/1900 in SQL but in PowerQuery it is 12/30/1899 so I use 2 instead of the 0.

    ISO Week = Number.RoundDown((Date.DayOfYear(Date.From(Duration.Days(([Date]-Date.From(2))/7)*7+5))+6)/7)
    

    I also needed the ISO Year so I made an adjustment to the ISO Week calculation and came up with:

    ISO Year = Date.Year(Date.From(Duration.Days(([Date]-Date.From(2))/7)*7+3))
    

    Change the [Date] to reference the date column in the data.

提交回复
热议问题