• I agree that your expected results don't match your sample data. But I think you're looking for something like this:

    SELECT id, cid, startdate=MIN(d)

    , enddate=CASE

    WHEN DATEPART(year, MIN(d)) = DATEPART(year, GETDATE()) THEN NULL

    WHEN DATEPART(year, MAX(d)) = DATEPART(year, MIN(d)) AND MAX(d) <> MIN(d) THEN MAX(d)

    ELSE DATEADD(year, 1, DATEADD(year, DATEDIFF(year, 0, MIN(d)), 0))-1 END

    FROM

    (

    SELECT id, cid, startdate, d

    ,rn=ROW_NUMBER() OVER (PARTITION BY id, cid ORDER BY d)/2

    FROM #table

    CROSS APPLY

    (

    VALUES (startdate),(startdate-1)

    ) b (d)

    ) a

    WHERE rn > 0

    GROUP BY id, cid, rn;

    Output from your sample data:

    id cid startdate enddate

    1 100 2013-06-04 00:00:00.000 2013-06-16 00:00:00.000

    1 100 2013-06-17 00:00:00.000 2013-06-24 00:00:00.000

    1 100 2013-06-25 00:00:00.000 2013-12-31 00:00:00.000

    1 100 2014-02-23 00:00:00.000 NULL

    2 200 2013-08-03 00:00:00.000 2013-08-08 00:00:00.000

    2 200 2013-08-09 00:00:00.000 2013-12-31 00:00:00.000


    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