我有一个存储的过程,它有一个用户输入的日期(YYYMMDD)。
要构建查询的最后一部分,我需要获取输入到 12 个月前的日期的日期。
我尝试了几种不同的方法,例如将其转换为 datetime 或 dateadd 函数,但仍然没有运气:下面是我的代码片段:
@DateSelected datetime=null,
@year int=null,
@week int=null,
@DayOfWeek int=null,
@DateSelectedLastYear datetime=null
--@Day int=null
as
begin
--if date parms are null get current week and year
if (@DateSelected is null)
begin
select @Year=year_number,@Week=week_number,@DayOfWeek=day_of_week from infrastructure..calendar
where calendar_date=DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()-1))
end
else
begin
select @Year=year_number,@Week=week_number,@DayOfWeek=day_of_week from infrastructure..calendar
where calendar_date=@DateSelected
end
begin
Select
@DateSelectedLastYear = DATEADD(YYYY,-1,@DateSelected)
end