SQL Query to find the last day of the month

前端 未结 14 1735
太阳男子
太阳男子 2020-11-27 04:40

I need to find the last day of a month in the following format:

\"2013-05-31 00:00:00:000\"

Anybody please help out.

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 05:15

    Try this one -

    CREATE FUNCTION [dbo].[udf_GetLastDayOfMonth] 
    (
        @Date DATETIME
    )
    RETURNS DATETIME
    AS
    BEGIN
    
        RETURN DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @Date) + 1, 0))
    
    END
    

    Query:

    DECLARE @date DATETIME
    SELECT @date = '2013-05-31 15:04:10.027'
    
    SELECT DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @date) + 1, 0))
    

    Output:

    -----------------------
    2013-05-31 00:00:00.000
    

提交回复
热议问题