Calculate the last day of the prior quarter

后端 未结 6 1319
别那么骄傲
别那么骄傲 2020-12-10 16:24

What\'s the most efficient way to calculate the last day of the prior quarter?

Example: given the date 11/19/2008, I want to return 9/30/2008.

Platform is S

6条回答
  •  忘掉有多难
    2020-12-10 17:09

    Get the current date

    SELECT CONVERT(DATE,GETDATE()) [Current Date]
    

    Get the 1st date of the quarter for the current date

    SELECT CONVERT(DATE, DATEADD(QQ, DATEDIFF(QQ, 0, GETDATE())  ,0)) [Current Quarter 1st Date]
    

    Get the last date of the quarter for the current date

    SELECT CONVERT(DATE,DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, GETDATE()) +1, 0))) [Current Quarter Last Date]
    

    Get the 1st date of the next quarter for the current date

    SELECT CONVERT(DATE, DATEADD(QQ, DATEDIFF(QQ, 0, GETDATE()) +1 ,0)) [Next Quarter 1st Date]
    

    Get the last date of the next quarter for the current date

    SELECT CONVERT(DATE,DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, GETDATE()) +2, 0))) [Next Quarter Last Date]
    

提交回复
热议问题