Get the last day of the month in SQL

后端 未结 20 2145
借酒劲吻你
借酒劲吻你 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:18

    using sql server 2005, this works for me:

    select dateadd(dd,-1,dateadd(mm,datediff(mm,0,YOUR_DATE)+1,0))
    

    Basically, you get the number of months from the beginning of (SQL Server) time for YOUR_DATE. Then add one to it to get the sequence number of the next month. Then you add this number of months to 0 to get a date that is the first day of the next month. From this you then subtract a day to get to the last day of YOUR_DATE.

提交回复
热议问题