1

How can I get the last day of the prior quarter?

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

I found this SQL-Script, but it doesn't work for me.

4

2 回答 2

2

The Script doesn't work in Sybase, because it can't compute the '0' you need the Unix-Time.

for example:

DECLARE @LastDay datetime
SELECT @LastDay = DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, '01.01.1970', GETDATE()), '01.01.1970'))
SELECT @LastDay

Today (11/07/2013) This will Output:

30.09.2013 00:00:00

the Date-format can change, depending on how your Server ist configured.

If you don't want the Time, you have to change the datatype of @LastDay to date

于 2013-11-07T14:00:35.320 回答
0

我创建了一个函数来在我的项目中使用它

CREATE FUNCTION preQtr
(@pQtr DATE)
RETURNS Date
AS

BEGIN
DECLARE @OUTPUT DATE

SET @OUTPUT = (select DATEADD(D, -1, DATEADD(qq, DATEDIFF(qq, 0,@pQtr), 0)))
RETURN @OUTPUT
END

select dbo.preQtr('2016-06-30')
select dbo.preQtr('2016-03-31')

此外,您可以在这样使用时获取日期时间:在此处输入图像描述

select DATEADD(s, -1, DATEADD(qq, DATEDIFF(qq, 0,'2016-03-31'), 0)) As PreviousQuarterLastdayTime
select DATEADD(s, -1, DATEADD(qq, DATEDIFF(qq, 0,'2016-03-31'), +1)) As QuarterFirstdayTime
于 2017-01-07T12:08:50.223 回答