Find last sunday

前端 未结 10 1312
余生分开走
余生分开走 2020-12-05 20:40

How will you find last sunday of a month in sql 2000?

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 21:16

    Here's the correct way, accounting for @@DATEFIRST

    IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fu_dtLastSundayInMonth]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    BEGIN
        EXECUTE(N'CREATE FUNCTION [dbo].[fu_dtLastSundayInMonth]() RETURNS int BEGIN RETURN 0 END ')
    END 
    GO 
    
    
    
    /*
    SET DATEFIRST 3; -- Monday
    
    WITH CTE AS (
    
        SELECT 1 AS i, CAST('20190101' AS datetime) AS mydate  
        UNION ALL 
    
        SELECT i+1 AS i, DATEADD(month, 1, CTE.mydate) AS mydate 
        FROM CTE WHERE i < 100 
    )
    
    SELECT -666 AS i, dbo.fu_dtLastSundayInMonth('17530101') AS lastSundayInMonth, dbo.fu_dtLastSundayInMonth('17530101') AS Control 
    
    UNION ALL 
    
    SELECT -666 AS i, dbo.fu_dtLastSundayInMonth('99991231') AS lastSundayInMonth, dbo.fu_dtLastSundayInMonth('99991231') AS Control 
    
    UNION ALL 
    
    SELECT 
         mydate 
        ,dbo.fu_dtLastSundayInMonth(mydate) AS lastSundayInMonth 
        ,dbo.fu_dtLastSundayInMonth(mydate) AS lastSundayInMonth 
        ,DATEADD(day,DATEDIFF(day,'19000107', DATEADD(MONTH, DATEDIFF(MONTH, 0, mydate, 30))/7*7,'19000107') AS Control 
    FROM CTE 
    
    */
    
    
    -- =====================================================================
    -- Description:   Return date of last sunday in month
    --                of the same year and month as @in_DateTime
    -- =====================================================================
    ALTER FUNCTION [dbo].[fu_dtLastSundayInMonth](@in_DateTime datetime )
    RETURNS DateTime
    AS
    BEGIN
        -- Abrunden des Eingabedatums auf 00:00:00 Uhr
        DECLARE @dtReturnValue AS DateTime  
        -- 26.12.9999   SO
        IF @in_DateTime >= CAST('99991201' AS datetime) 
            RETURN CAST('99991226' AS datetime); 
    
        -- @dtReturnValue is now last day of month 
        SET @dtReturnValue = DATEADD 
            (
                 DAY 
                ,-1
                ,DATEADD
                (
                     MONTH
                    ,1
                    ,CAST(CAST(YEAR(@in_DateTime) AS varchar(4)) + RIGHT('00' + CAST(MONTH(@in_DateTime) AS varchar(2)), 2) + '01' AS datetime) 
                )
            )
        ;
    
        -- SET DATEFIRST 1 -- Monday - Super easy ! 
        -- SET DATEFIRST != 1 - PHUK THIS ! 
        SET @dtReturnValue = DATEADD
                            (
                                day
                                ,
                                 -
                                 (
    
                                    (
                                        -- DATEPART(WEEKDAY, @lastDayofMonth) -- with SET DATEFIRST 1 
                                        DATEPART(WEEKDAY, @dtReturnValue) + @@DATEFIRST - 2 % 7 + 1 
                                    )
                                    %7
                                )
                                , @dtReturnValue
        );
    
        RETURN @dtReturnValue; 
    END
    
    
    GO
    

提交回复
热议问题