1

我有一个存储的过程,它有一个用户输入的日期(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
4

2 回答 2

2

试试这个:

select dateadd(year, -1, getdate())
于 2013-11-20T13:50:40.963 回答
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
于 2013-11-20T14:04:40.503 回答