I have a simple data set shown in Table 1 below. I would like to produce two new columns (START_SHIFT_DT_TIME and END_SHIFT_DT_TIME) using the columns from Table 1. The desired output is shown in Table 2.
The requirements are:
- If the
START_SHIFThours starts on or after 22:00:00 then the actual start date is the previous day. - If the
START_SHIFThours starts before 22:00:00 then the actual start date is the following day.
I am using SQL server.
Table 1:
START_SHIFT END_SHIFT DATE
22:00:00 06:00:00 1/13/2012 12:00:00 AM
07:00:00 15:00:00 1/13/2012 12:00:00 AM
23:30:00 07:30:00 2/27/2012 12:00:00 AM
00:00:00 08:00:00 2/17/2012 12:00:00 AM
17:00:00 01:00:00 1/1/2012 12:00:00 AM
Table 2:
START_SHIFT END_SHIFT DATE START_SHIFT_DT_TIME END_SHIFT_DT_TIME
22:00:00 06:00:00 1/13/2012 12:00:00 AM 1/12/2012 10:00:00 PM 1/13/2012 6:00:00 AM
07:00:00 15:00:00 1/13/2012 12:00:00 AM 1/13/2012 7:00:00 AM 1/13/2012 3:00:00 PM
23:30:00 07:30:00 2/27/2012 12:00:00 AM 2/26/2012 11:30:00 PM 2/27/2012 7:30:00 AM
00:00:00 08:00:00 2/17/2012 12:00:00 AM 2/17/2012 12:00:00 AM 2/17/2012 8:00:00 AM
17:00:00 01:00:00 1/1/2012 12:00:00 AM 1/1/2012 5:00:00 PM 1/2/2012 1:00:00 AM
The pattern used in the last column is that the time component in
END_SHIFTis converted to seconds and then added to theDATE. In the 4th column, the same pattern is used and it goes through an additional step of adding -1 day (previous) whenSTART_SHIFTis 22:00 or later.