Get the last day of the month in SQL

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

    WinSQL: I wanted to return all records for last month:

    where DATE01 between dateadd(month,-1,dateadd(day,1,dateadd(day,-day(today()),today()))) and dateadd(day,-day(today()),today())
    

    This does the same thing:

    where month(DATE01) = month(dateadd(month,-1,today())) and year(DATE01) = year(dateadd(month,-1,today()))
    
    0 讨论(0)
  • 2020-11-27 05:27

    Try to run the following query, it will give you everything you want :)

    Declare @a date =dateadd(mm, Datediff(mm,0,getdate()),0)
    Print('First day of Current Month:')
    Print(@a)
    Print('')
    set @a = dateadd(mm, Datediff(mm,0,getdate())+1,-1)
    Print('Last day of Current Month:')
    Print(@a)
    Print('')
    
    Print('First day of Last Month:')
    set @a = dateadd(mm, Datediff(mm,0,getdate())-1,0)
    Print(@a)
    Print('')
    
    Print('Last day of Last Month:') 
    set @a = dateadd(mm, Datediff(mm,0,getdate()),-1)
    Print(@a)
    Print('')
    
    
    Print('First day of Current Week:')
    set @a = dateadd(ww, Datediff(ww,0,getdate()),0)
    Print(@a)
    Print('')
    
    Print('Last day of Current Week:')
    set @a = dateadd(ww, Datediff(ww,0,getdate())+1,-1)
    Print(@a)
    Print('')
    
    Print('First day of Last Week:')
    set @a =  dateadd(ww, Datediff(ww,0,getdate())-1,0)
    Print(@a)
    Print('')
    
    Print('Last day of Last Week:')
    set @a =  dateadd(ww, Datediff(ww,0,getdate()),-1)
    Print(@a)
    
    0 讨论(0)
提交回复
热议问题