SQL query to find last day of current month?

后端 未结 3 1282
深忆病人
深忆病人 2020-12-29 16:05
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
 LastDay_CurrentMonth

Hi everyone I have a query to find the last day of current mont

相关标签:
3条回答
  • 2020-12-29 16:29

    This will give you the last day of current month but ignores time

    select EOMONTH(GETDATE())

    From Microsoft tech net

    0 讨论(0)
  • 2020-12-29 16:30
    CREATE FUNCTION  EOMONTH
    (
        @date datetime,
        @months int
    )
    RETURNS datetime
    AS
    BEGIN
         declare @eom datetime
         declare @d datetime
         set @d = dateadd(MONTH, @months, @date)
         select @eom =   dateadd(SECOND,-1,DATEADD(MONTH,datediff(MONTH,0,@d)+1,0))
        RETURN  @eom 
    
    END
    GO
    
    0 讨论(0)
  • 2020-12-29 16:45

    Get the DateTime of Now

    GETDATE() -- 2011-09-15 13:45:00.923
    

    Calculate the difference in month's from '1900-01-01'

    DATEDIFF(m, 0, GETDATE()) -- 1340
    

    Add the difference to '1900-01-01' plus one extra month

    DATEADD(m, DATEDIFF(m, 0, GETDATE())+1, 0) -- 2011-10-01 00:00:00.000
    

    Remove one second

    DATEADD(s, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE())+1, 0)) -- 2011-09-30 23:59:59.000
    
    0 讨论(0)
提交回复
热议问题