Skip to content
Advertisement

UNPIVOT Holiday Hours

I have a table, that keeps track of store holiday hours:

This is a highly oversimplified table. There’s about 30 columns horzontially consisting of store operating hours by date – It continues (DATE3, TIMES3, DATE4, TIMES4, etc).

I need to unpivot the values vertically, ensuring the date and time values are on the same record.

(NOTE: Once I figure out to structure the UNPIVOT expression properly, I will use Dynamic SQL on my own to pivot the column names)

Desired Outcome:

I tried using UNPIVOT, but I’m stuck. Any ideas?

SAMPLE DATA:

Code that I tried:

Advertisement

Answer

Gordon is 100% correct (+1).

However, if you are looking for a dynamic approach WITHOUT having to use Dynamic SQL, consider the following.

Example

Returns

The Table-Valued Function if Interested

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement