我有一个存储过程,其用户输入日期(YYYMMDD)。
要构建查询的最后部分,我需要获取12个月前输入日期的日期。
我尝试了一些不同的方法,例如将其作为日期时间或日期添加函数进行投射但仍然没有运气:下面是我的代码片段:
@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
答案 0 :(得分:2)
试试这个:
select dateadd(year, -1, getdate())
答案 1 :(得分:1)
试试这个
@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