Home Forums SQL Server 2008 SQL Server Newbies splitting up a start time and end time into different dates RE: splitting up a start time and end time into different dates

  • meltingchain (6/20/2013)


    Kingston Dhasian (6/20/2013)

    Great job posting the sample data and the structure of the table:-)

    Can you also provide the expected results based on your sample data

    That will help us to test the solution ourselves before we post

    The What I Tried section comes close to how i want it to output. But notice the date 2013-06-03 is spanning 3 days. This isn't shown in that. Instead i need the output to look like the result from this table

    create Table #tempOutput(datestart date, AM int, PM int)

    insert Into #TempOutput (dateStart,AM,PM)

    Select '2013-06-03',4,5 Union ALL --first day between 2013-06-03, 2013-06-05 hours split between AM and PM

    Select '2013-06-04',4,5 Union ALL --Second day between 2013-06-03, 2013-06-05 hours split between AM and PM

    Select '2013-06-05',4,5 Union ALL --Third day between 2013-06-03, 2013-06-05 hours split between AM and PM

    Select '2013-06-07',0,1 Union ALL -- Time between 2013-06-07 14:00:00.000, 2013-06-07 15:00:00.000 hours split between AM and PM

    Select '2013-06-08',1,0 Union ALL -- Time between 2013-06-08 08:00:00.000, 2013-06-08 09:00:00.000 hours split between AM and PM

    Select '2013-06-27',4,5 Union ALL --first day between 2013-06-27, 2013-06-28 hours split between AM and PM

    Select '2013-06-28',4,5 Union ALL --Second day between 2013-06-27, 2013-06-28 hours split between AM and PM

    Are all days considered to only be between 8am and 5pm?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/