Wednesday, December 31, 2014

How to Convert Columns into Row in SQL server.

Sometime we need to show data in rows that saved in different columns.  For that purpose we can use two relational operators UNPIVOT and PIVOT to convert data into rows from columns and vice versa.  

In my example, I have a table named Finger_Print that saved each finger in different columns. Now, I need to iterate each finger against every person. Here I convert those columns into rows. This may help you.


   select ID,Finger as FingerPrint
   from (
                  select Criminal_ID,RightThumb, RightIndex, RightMiddle, RightRing, RightLittle from Finger_Print
            )   Temp unpivot
            (
                  Finger for FingerPrint in (RightThumb, RightIndex, RightMiddle, RightRing, RightLittle)

            )PVT

No comments:

Post a Comment

Search This Blog