转发依赖关系@startdate @ enddate = @startdate + 1周

时间:2015-09-07 07:21:41

标签: sql tsql visual-studio-2012 parameters

我正在寻找一种方法,如果用户选择

@startdate = '12-01-2015'
then
@enddate = @startdate + 1 Week

这是我的参数,但请注意我的报告是一个矩阵,每周使用一个数据样本,所以参数需要自动选择 它从所有选定的开始日期结束

开始于('12 -01-2015','19 -01-2015')

然后在('18 -01-2015','26 -01-2015')

结束

这是我用于我的startdate参数

的内容
select distinct
CAST(startdate AS DATE) as Startdate
FROM [Calendar] 
where DatePart(YEAR,startdate)  >= 2014
order by 1 asc

1 个答案:

答案 0 :(得分:0)

使用DATEADD

@startdate = '12-01-2015'
then
@enddate = DATEADD(wk, 1, @startdate)

或者如果startdate变量有DATE/DATETIME类型

@startdate = '12-01-2015'
then
@enddate = @startdate + 7;   -- Implicit cast add 7 days

编辑:

select distinct
    CAST(startdate AS DATE) as Startdate,
    DATEADD(wk, 1, CAST(startdate AS DATE)) as EndDate
FROM [Calendar] 
where DatePart(YEAR,startdate)  >= 2014
order by 1 asc