• Create a reference or tally table that holds all the dates of the period you need (i.e. 365 rows per year). Select the desired date range from this reference table with a simple SELECT and WHERE clause. You can JOIN the results with other tables when needed. You can also create a stored procedure (or TVF) from the code below.

    DECLARE @ShiftDate date

    SET @ShiftDate = '20130202'

    SELECT datecolumn

    FROM referencetable

    WHERE datecolumn <= @ShiftDate

    -- unmark the row below to get all dates from the first day of the year

    --AND datecolumn >= CAST(YEAR(@ShiftDate) as CHAR(4)) + '0101'

    -- unmark the row below to get all dates from the first day of the month

    --AND datecolumn >= CAST(YEAR(@ShiftDate) as CHAR(4)) + RIGHT('00' + CAST(MONTH(@ShiftDate) as varchar(2)), 2) + '01'

    -- unmark the row below to get all dates from the first day of the week

    --AND datecolumn >= dateadd(day, -1 * (DATEPART(weekday, @ShiftDate))+1, @ShiftDate)

    ORDER BY datecolumn

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **