SQL Query to find the last day of the month

前端 未结 14 1551
太阳男子
太阳男子 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:18
    Declare @GivenDate datetime 
    Declare @ResultDate datetime 
    DEclare @EOMDate datetime 
    Declare @Day int 
    set @GivenDate=getdate() 
    set @GivenDate= (dateadd(mm,1,@GivenDate)) 
    set @Day =day(@GivenDate) 
    set @ResultDate=dateadd(dd,-@Day+1,@GivenDate) 
    select @EOMDate =dateadd(dd,-1 ,@ResultDate) 
    select @EOMDate 
    
    0 讨论(0)
  • 2020-11-27 05:22

    In Snowflake (and likely other SQL engines), you can use LAST_DAY.

    select to_date('2015-05-08T23:39:20.123-07:00') as "DATE",
           last_day("DATE", 'MONTH') as "LAST DAY OF MONTH";
    

    Which returns:

    DATE         LAST DAY OF MONTH
    2015-05-08          2015-05-31
    
    0 讨论(0)
  • 2020-11-27 05:24

    select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-0, -1) LastDate

    0 讨论(0)
  • 2020-11-27 05:25

    An excelent approach by me. Regards

    DECLARE @MAXDATE INT=(SELECT MAX(DATEPART(YEAR,ORDERDATE)) FROM Orders)
    DECLARE @MINDATE INT=(SELECT MIN(DATEPART(YEAR,ORDERDATE)) FROM Orders)
    DECLARE @HORA INT=(SELECT MIN( DATEPART(HOUR,ORDERDATE)) FROM ORDERS)
    DECLARE @DIA INT = 28
    
    SELECT Employees.EmployeeID , Orders. OrderID , OrderDate  FROM Employees
    INNER JOIN Orders 
    ON Employees.EmployeeID = Orders.EmployeeID
    Where  (DATEPART(YEAR,ORDERDATE)) >=@mindate and  (DATEPART(YEAR,ORDERDATE))<= @maxdate
    and DATEPART(HOUR,ORDERDATE)=@HORA   and DATEPART(DAY,ORDERDATE) IN (30,31) OR DATEADD(DAY,0,DATEPART(DAY,ORDERDATE))=28 AND
    DATEADD(MONTH,0,DATEPART(MONTH,ORDERDATE))=2
    ORDER BY 1 ASC
    
    0 讨论(0)
  • 2020-11-27 05:26
    dateadd(month,1+datediff(month,0,getdate()),-1)
    

    To check run:

    print dateadd(month,1+datediff(month,0,@date),-1)
    
    0 讨论(0)
  • 2020-11-27 05:27

    Calculate the last date of the month is quite simple calculation -

    1 - Find the total months count till today's date using DATEDIFF function -

    Select DATEDIFF(MM,0,GETDATE())
    

    Output - 1374, If getdate() output is "2014-07-23 19:33:46.850"

    2 -Increment by 1 into total months count -

    Select DATEDIFF(MM,0,GETDATE())+1
    

    Output - 1375, If getdate() output is "2014-07-23 19:33:46.850"

    3 - Get the first date of next month -

    Select DATEADD(MM,DATEDIFF(MM,0,GETDATE())+1,0)
    

    Output - '2014-08-01 00:00:00.000', If getdate() output is "2014-07-23 19:33:46.850"

    4 - Subtract by -1 into the first date of next month, which will return last date of the current month -

    Select DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,0,GETDATE())+1,0))
    

    Output - '2014-07-31 00:00:00.000', If getdate() output is "2014-07-23 19:33:46.850"

    In the same manner of calculation we can achieve the -

    1. Last date of next month
    2. Last date of the previous month and so on.
    0 讨论(0)
提交回复
热议问题