Get the last day of the month in SQL

后端 未结 20 2201
借酒劲吻你
借酒劲吻你 2020-11-27 04:49

I need to get the last day of the month given as a date in SQL. If I have the first day of the month, I can do something like this:

DATEADD(DAY, DATEADD(MONT         


        
20条回答
  •  青春惊慌失措
    2020-11-27 05:14

    Here's my version. No string manipulation or casting required, just one call each to the DATEADD, YEAR and MONTH functions:

    DECLARE @test DATETIME
    SET @test = GETDATE()  -- or any other date
    
    SELECT DATEADD(month, ((YEAR(@test) - 1900) * 12) + MONTH(@test), -1)
    

提交回复
热议问题