【数据库】sqlserver 指定季度,求季度末日期
程序员文章站
2022-05-15 10:58:19
...
DECLARE @to_month VARCHAR(2),
@to_quarter INT, -- 季度
@to_year varchar(4), -- 年份
@quarter_end datetime -- 季度末日期
SET @to_quarter=4
SET @to_year=2020
SET @to_month= CASE WHEN @to_quarter=1 THEN '3'
WHEN @to_quarter=2 THEN '6'
WHEN @to_quarter=3 THEN '9'
WHEN @to_quarter=4 THEN '12'
END
set @quarter_end= DATEADD(QUARTER,DATEDIFF(QUARTER,0,CONVERT(DATE,@to_year+'-'[email protected]_month+'-1',20))+1,-1)
SELECT @to_year 年份,@to_quarter 季度,@to_month 月份,@quarter_end 季度末日期