• sgmunson (7/10/2014)


    There's a much simpler query:

    DECLARE @DATE AS datetime = '07/10/2014';

    DECLARE @TIME_START_INT AS int = 820;

    DECLARE @TSV AS varchar(4) = RIGHT('00' + CAST(@TIME_START_INT AS varchar(4)), 4);

    DECLARE @TIME_END_INT AS int = 1000;

    DECLARE @TEV AS varchar(4) = RIGHT('00' + CAST(@TIME_END_INT AS varchar(4)), 4);

    DECLARE @START AS datetime = @DATE + CAST(LEFT(@TSV, 2) + ':' + RIGHT(@TSV, 2) + ':00' AS time);

    DECLARE @END AS datetime = @DATE + CAST(LEFT(@TEV, 2) + ':' + RIGHT(@TEV, 2) + ':00' AS time);

    DECLARE @INTERVAL AS int = 20;

    WITH Tally AS (

    SELECT 1 AS N

    UNION ALL

    SELECT N + 1

    FROM Tally

    WHERE N + 1 < 10001

    )

    SELECT DATEADD(mi, @INTERVAL * N, @START) AS THE_TIME

    FROM Tally

    WHERE DATEADD(mi, @INTERVAL * N, @START) <= @END

    OPTION (MAXRECURSION 10000)

    Janet Barnett (7/10/2014)


    Looking for assistance in returning multiple entries from a time span. I have a date, start-time, end-time and duration. I need the start-times separated in a list. It's fine if temp tables are needed - I have that clearance.

    Entry might look like:

    (datetime) date: 7/10/2014

    (int) start-time 820

    (int) end-time 1000

    (int) duration 20

    The result would needed:

    (datetime) 7/10/2014 08:20:00

    (datetime) 7/10/2014 08:40:00

    (datetime) 7/10/2014 09:00:00

    (datetime) 7/10/2014 09:20:00

    (datetime) 7/10/2014 09:40:00

    Oh... be careful now... Simpler isn't always better. That uses an rCTE that "Counts" and it's a known performance and resource usage issue. There are many other much better methods some of which are even "simpler". Please see the following article for what I'm talking about on the rCTE that "Counts" thing...

    http://www.sqlservercentral.com/articles/T-SQL/74118/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)