我有一个存储过程,它有一个日期(YYYYMDD)的用户输入。
为了构建查询的最后一部分,我需要获得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
试试这个:
select dateadd(year, -1, getdate())
试试这个
@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(YEAR,-1,@DateSelected)
end