• Assuming you're using a TIME field to store your values, you can use a Tally table and the built-in DATEADD/DATEDIFF functions for this:

    DECLARE @t TABLE

    (OfficeId INT, WeekdayId INT, StartTime TIME, EndTime TIME)

    INSERT INTO @t

    SELECT 1, 2, '14:30', '16:30'

    UNION ALL SELECT 1, 3, '16:00', '18:00'

    ;WITH Tally (n) AS (

    SELECT TOP 100 30*(ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1)

    FROM sys.all_columns)

    SELECT OfficeID, WeekdayID

    ,TSStart=DATEADD(minute, n, StartTime)

    ,TSEnd=DATEADD(minute, n + 30, StartTime)

    ,Timeslot=CONVERT(VARCHAR(100), DATEADD(minute, n, StartTime), 0) + ' - ' +

    CONVERT(VARCHAR(100), DATEADD(minute, n + 30, StartTime), 0)

    FROM @t

    CROSS APPLY (

    SELECT n

    FROM Tally

    WHERE n BETWEEN 0 AND DATEDIFF(minute, StartTime, DATEADD(minute, -30, EndTime))) a

    ORDER BY OfficeID, WeekdayID, TSStart


    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