SQL 计算季度的最后一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:17:39  来源:igfitidea点击:

Calculate the last day of the quarter

sqlsql-serverdate

提问by Bob Probst

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.

示例:给定日期 11/19/2008,我想返回 9/30/2008。

Platform is SQL Server

平台是 SQL Server

回答by Charles Bretana

If @Date has the date in question

如果@Date 有相关日期

Select DateAdd(day, -1, dateadd(qq, DateDiff(qq, 0, @Date), 0)) 

EDIT: Thanks to @strEagle below, simpler still is:

编辑:感谢下面的@strEagle,更简单的仍然是:

Select dateadd(qq, DateDiff(qq, 0, @Date), -1) 

回答by StrEagle

Actually simpler is:

其实更简单的是:

SELECT DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), -1)

回答by akila

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]

回答by Bob Probst

I came up with this (tested for all months):

我想出了这个(测试了所有月份):

select dateadd(dd,-1,dateadd(qq,datediff(qq,0,'11/19/2008'),0)),
       dateadd(dd,-1,dateadd(qq,datediff(qq,0,'10/19/2008'),0)),
       dateadd(dd,-1,dateadd(qq,datediff(qq,0,'12/19/2008'),0))

It might turn out to be the simplest.

这可能是最简单的。

回答by ManhNguyen

convert(varchar, dateadd(dd,-1,dateadd(qq,1,DATEADD(qq, DATEDIFF(qq,0,YOUR_DATE), 0))),112)

you also can change 112 base on this below list

您也可以根据以下列表更改 112

SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)
                                        -- Oct  2 2008 11:01AM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2008                  
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd – 2008.10.02           
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) --  hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
                                        -- Oct  2 2008 11:02:44:013AM   
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
                                        --  02 Oct 2008 11:02:07:577     
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) --  yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
                                        --  2008-10-02T10:52:47.513
-- SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), '/', ' ') -- yyyy mm dd
SELECT convert(varchar(7), getdate(), 126)                 -- yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8)          -- mon yyyy

回答by Amiram Pick

In amazon redshift:

在亚马逊红移中:

Last day of the previous quarter:

上一季度的最后一天:

select dateadd(day,-1,DATE_TRUNC('qtr', current_date)) from whatever

Last day of current quarter:

本季度的最后一天:

select dateadd(qtr,1,dateadd(day,-1,DATE_TRUNC('qtr', current_date))) from whatever