如果给我一个约会日期(比如@d = '11 -25-2010'),我如何确定从该日期开始的季度结束。我想在午夜前一秒使用时间戳。
我可以得到这个:
select dateadd(qq, datediff(qq, 0, getdate()), 0) as quarterStart
给了我:'10 -1-2010'
我在一天的午夜之前使用它一秒钟:
select DateAdd(second, -1, DateAdd(day, DateDiff(day, 0, @d))+1, 0) ) AS DayEnd
最后,一个quarterEnd方法会给我'12 -31-2010 23:59:00'
答案 0 :(得分:2)
我想我明白了:
select DateAdd(second, -1, DateAdd(qq, DateDiff(qq, 0, getdatE())+1, 0) )
答案 1 :(得分:2)
这是在这里被问到并回答的:http://ask.sqlservercentral.com/questions/6680/how-to-find-quarter-starting-date-and-end-date
答案 2 :(得分:1)
Select Dateadd(qq, datediff(qq, 0, getdate()), 0) as QuarterStart
, DateAdd(d, -1
, DateAdd(qq, 1
, dateadd(qq, datediff(qq, 0, getdate()), 0))) As QuarterEnd
找到随后季度的第一天,减去一天。如果你在一个范围内使用它,那么使用“严格小于”并且不要减去一天:
MyDate >= Dateadd(qq, datediff(qq, 0, getdate()), 0)
And MyDate < DateAdd(qq, 1
, dateadd(qq, datediff(qq, 0, getdate()), 0))
答案 3 :(得分:0)
试试这个
select dateadd(second,-1,
dateadd(month,3,
dateadd(qq, datediff( quarter , 0 , current_timestamp ) ,
0
)
)
)
答案 4 :(得分:0)
为了清晰起见,非常冗长。基本上它是直接的方法:
所以这是代码:
declare @current datetime
set @current = '9-25-2010'
declare @year int
set @year = datepart(year, @current)
declare @quartermonth int
select @quartermonth = ((datepart(month, @current) - 1)/ 3 + 1)* 3 + 1 -- go to the next month
select @year, @quartermonth
-- adjust year if it's 4th quater
select @year = @year + @quartermonth / 12
-- avoid month overflow
select @quartermonth = @quartermonth % 12
select @year, @quartermonth
declare @firstdayofnextquater datetime
select @firstdayofnextquater = convert(datetime, convert(varchar, @year) + '-' + convert(varchar, @quartermonth) + '-01')
select dateadd(second, -1, @firstdayofnextquater)