• Steven,

    Not sure why you used all of those yukky loops (reference my mantra):

    IF OBJECT_ID('tempdb..#Years') IS NOT NULL

    DROP TABLE #Years

    --a table to hold the sample data

    CREATE TABLE #Years (

    [ID] INT IDENTITY(1,1) NOT NULL,

    [StartYear] DATE NULL,

    [EndYear] DATE NULL,

    PRIMARY KEY (ID))

    DECLARE @NumSampleRows INT

    SET @NumSampleRows = 10

    ;WITH Tally (n) AS (

    SELECT TOP (@NumSampleRows) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM sys.all_columns)

    INSERT INTO #Years

    SELECT [StartYear]

    ,[EndYear]=DATEADD(day, ABS(CHECKSUM(NEWID())) % 6000, [StartYear])

    FROM Tally

    CROSS APPLY (SELECT [StartYear]=DATEADD(day, ABS(CHECKSUM(NEWID())) % 50000, 0)) a

    SELECT ID, [StartYear], [EndYear], OddStr=(

    SELECT CASE n WHEN 0 THEN '' ELSE '/' END +

    CAST(YEAR(DATEADD(year, n, StartYear))%10 AS VARCHAR)

    FROM (

    SELECT 0 UNION ALL SELECT TOP (DATEDIFF(year, [StartYear], [EndYear]))

    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM sys.all_columns) a(n)

    ORDER BY n

    FOR XML PATH(''))

    FROM #Years

    GROUP BY ID, [StartYear], [EndYear]

    IF OBJECT_ID('tempdb..#Years') IS NOT NULL

    DROP TABLE #Years

    Edit: Fixed a minor compatibility issue with SQL 2005.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St