What is ISO_year in sql-server

后端 未结 2 856
刺人心
刺人心 2020-12-03 16:29

I am currently working on creating my first Data_warehouse using sql-server

I have a Date dimension i want to populate it using SSIS it ha

相关标签:
2条回答
  • 2020-12-03 17:00

    Here is a function for iso_year, the logic behind it is that the thursday of the week from the parameter date determine the year:

    CREATE FUNCTION [dbo].[f_isoyear]
    (
    @p_date datetime
    )
    RETURNS int
    as
    BEGIN
      RETURN datepart(yy, dateadd(wk, datediff(d, 0, @p_date)/7, 3))
    END
    
    0 讨论(0)
  • 2020-12-03 17:12

    Here is a Connect item that requests a function to calculate ISO_YEAR.

    DATEPART - ISO_YEAR for ISO_WEEK

    In the workaround section you have this function that you can use.

    CREATE FUNCTION [dbo].[ISOyear](@date DATETIME)
    returns SMALLINT
    AS
    BEGIN
         DECLARE @isoyear SMALLINT = CASE
             WHEN Datepart(isowk, @date)=1
                 AND Month(@date)=12 THEN Year(@date)+1
             WHEN Datepart(isowk, @date)=53
                 AND Month(@date)=1 THEN Year(@date)-1
             WHEN Datepart(isowk, @date)=52
                 AND Month(@date)=1 THEN Year(@date)-1             
             ELSE Year(@date)
            END;
         RETURN @isoyear;
    END;
    
    0 讨论(0)
提交回复
热议问题