I have 2 variables year and quarter that I get from the view. I have to pass these values to get the last date of that particular Quarter and Year. Is there a way to do that in SQL?
For example:
select (QuarterEndDate) where Year = @year and Quarter = @quarter
I would use datefromparts()
and eomonth()
:
select eomonth(datefromparts(@year, 3 * @quarter, 1))
declare @year int;
declare @quarter int;
set @year = 2020;
set @quarter = 3;
select eomonth(datefromparts(@year, 3 * @quarter, 1)) last_quarter_day
GO
| last_quarter_day | | :--------------- | | 2020-09-30 |
Try the following, should give you the last date of the quarter.
first option: qq
is your quarter number.
dateadd(qq, DateDiff(qq, 0, @Date), -1)
second option:
dateadd(D,-1,dateadd(M, 3*qq+3, CONVERT(date, CONVERT(varchar(5),@year)+'-1-1')))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.