获取2个提供日期之间的所有年份

时间:2015-04-10 06:28:55

标签: sql sql-server tsql

如果我有两年,例如:

YEAR(@STARTDATE) -- 2011
YEAR(@ENDDATE)   -- 2015

如何创建名为#YEARRANGE的临时表,其中包含所提供的两年之间的所有年份,包括@STARTDATE@ENDDATE。换句话说,它看起来像这样:

#YEARRANGE
==========
- 2011
- 2012
- 2013
- 2014
- 2015

7 个答案:

答案 0 :(得分:0)

您可以尝试这样:

DECLARE 
      @start DATE = '20120201'
    , @end DATE = '20150405'

;WITH cte AS 
(
    SELECT YEAR(@start) AS [Year]
    UNION ALL
    SELECT yl.[Year]+ 1 as [Year]
    FROM cte  yl
    WHERE yl.[year] + 1 <= Year(@end)
)
SELECT CONVERT(CHAR(4), [Year], 120) 
FROM cte

答案 1 :(得分:0)

CREATE TABLE #YEARRANGE
(
    Years INT
)

DECLARE @STARTDATE DATE,
        @ENDDATE DATE;
SET @STARTDATE = '20110101';
SET @ENDDATE = '20150101';

WITH GetYears 
AS
(
    SELECT YEAR(@STARTDATE) AS Years
    UNION ALL
    SELECT yl.Years+ 1 as Years
    FROM GetYears yl
    WHERE yl.Years + 1 <= YEAR(@ENDDATE)
)
INSERT INTO #YEARRANGE
SELECT Years FROM GetYears ORDER BY Years;

SELECT * FROM #YEARRANGE

DROP TABLE #YEARRANGE

输出:

year
2011
2012
2013
2014
2015

答案 2 :(得分:0)

DECLARE @STARTDATE DATE, @ENDDATE DATE

SET @STARTDATE = '2011-01-10'
SET @ENDDATE = '2015-06-16'

CREATE TABLE #YEARRANGE([YEAR] INT)

;WITH C AS (
        SELECT  DATEPART(YEAR, @STARTDATE) AS yr
        UNION ALL
        SELECT yr + 1
        FROM C
        WHERE yr < DATEPART(YEAR, @ENDDATE)
        )
INSERT INTO #YEARRANGE
SELECT yr
FROM C
ORDER BY yr

SELECT * FROM #YEARRANGE

输出

YEAR
------------
2011
2012
2013
2014
2015

答案 3 :(得分:0)

DECLARE @STARTDATE INT  = 2011

DECLARE @ENDDATE INT = 2015

CREATE TABLE #YEARRANGE([YEAR] INT)

WHILE @STARTDATE <= @ENDDATE
BEGIN
    INSERT INTO #YEARRANGE
    VALUES(@STARTDATE)

    SET @STARTDATE = @STARTDATE + 1
END


SELECT * FROM #YEARRANGE

DROP TABLE #YEARRANGE

答案 4 :(得分:0)

以下是使用Tally Table的另一种解决方案:

DECLARE @startDate DATE = '20110101'
DECLARE @endDate DATE = '20150101'

;WITH E1(N) AS(
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)
,E2(N) AS(SELECT 1 FROM E1 a, E1 b)
,E4(N) AS(SELECT 1 FROM E2 a, E2 b)
,Tally(N) AS(
    SELECT TOP(DATEDIFF(YEAR, @startDate, @endDate) + 1) 
        ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
    FROM E4
)
SELECT
    YEAR(@startDate) + (N-1)
FROM Tally
WHERE
    YEAR(@startDate) + (N-1) <= YEAR(@endDate)

答案 5 :(得分:0)

您可以使用递归cte执行此操作,然后使用select into语句直接创建临时表,该语句会自动创建临时表而不显式创建最后一个:

DECLARE @sd INT = 2011, @ed INT = 2015

;WITH cte AS(SELECT @sd AS y
             UNION ALL
             SELECT y + 1 FROM cte WHERE y < @ed)
SELECT * INTO #YEARRANGE FROM cte

答案 6 :(得分:0)

Welp,有人可能会打败我这种变化。

Declare @StartYear int = 2011
    , @EndYear int = 2015
Declare @YearDiff int = @EndYear - @StartYear 
    , @i int =0 --Counter

if OBJECT_ID('tempdb..#YEARRANGE') is not null
    drop table #YEARRANGE;
Create Table #YEARRANGE (YearRange int)

-- I hate loops in SQL.
while @i <= @YearDiff
begin
    INSERT INTO #YEARRANGE
    select @i + @StartYear
    set @i +=1;
end

/*  Tada    */
Select * 
from #YEARRANGE