Search code examples
sqlsql-serverunpivot

Insert Data from old SQL table, to new one with columns changed to rows


I currently have a table that looks similar to this:

Old Table(example only)

I need to import the data to a new table, but making the column headers into rows and value for each one. Similar to this.

New Table(example only)

Would I use a Pivot or an Unpivot to do this? I have a lot of columns with about 700 rows that need to be converted this way.


Solution

  • You should go with unpivot, try the query below. Generally speaking, Pivot reduces the total number of rows while Unpivot increases it, instead.

    select SN,Property,Value
    
    from tbl
    
    unpivot
    
    (Value for Property in (Property1,Property2,Property3,Property4) ) as tblunpvt