Check out my SQLFiddle here (link)
In SQL Server 2008, I have table of event start and end times, like the image below.

I need to write a query that allows me to figure out how much of each event fell into which work shift. Our shifts are 12 hrs long and go from 06:00-18:00 and 18:00-06:00.
The query should produce results like the image below.

From that, I can then figure out total event durations for a particular work shift.
What can I do to go from the first image, to the second image?
SQL Fiddle version
In this solution, I generated a calendar of every shift’s start and end date. You can extend the calendar by changing
Where ShiftEnd < '2012-05-30'to a larger date. Note that if you end up with more than 50 shifts or so, you’ll want to addOption(Maxrecursion 0);to end of the query to lift SQL Server’s cap.