[英]SQL List of year from this year to 5 years back, then used as parameter in stored sproc
First I need to create a sproc that will give me just a list of years from this year back to 5 years before this year. 首先,我需要创建一个存储过程,以仅列出从今年到今年的5年的年份。 I was thinking of somehow using a table variable?
我正在考虑以某种方式使用表变量?
After that I need to use that value as a parameter in another sproc. 之后,我需要将该值用作另一个存储过程中的参数。 How would I then say show all Dates that contain the year that is picked?
那我要怎么说显示所有包含所选择年份的日期呢? I know how to write the parameter and everything I just need the Where clause part of it.
我知道如何编写参数,而我只需要其中的Where子句部分。
The date in the column are like this 2010/01/30 00:00.000 列中的日期是这样的2010/01/30 00:00.000
Part One: 第一部分:
Declare @Years Table (int year primary Key Not Null)
Declare @Yr integer Set @Yr = Year(getDate())
Declare @tYr Integer Set @tYr = @Yr
while @Yr > @TYr - 5 Begin
Insert @Years(year) Values (@Yr)
Set @Yr = @Yr - 1
End
Part TWO: (I assume you mean "Show all Dates That contain a year in the five year span" ??) 第二部分:(我假设您的意思是“显示五年跨度中包含一年的所有日期”?)
Where Year(dateColumn) In (Select year from @Years)
-- But this is not SARGable, so I would reccomend instead, -但是这不可行,所以我建议
Where dateColumn Between DateAdd(year, Year(getDate())-1905, 0)
And DateAdd(year, Year(getDate())-1899, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.