0

How to convert columns to rows in SQL Server?

For example I have table with 31 columns as numbers from 1 to 31.

PFA screenshot for the same.

These no should range from 1 to 31 in row format.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
rahul
  • 59
  • 2
  • 10
  • 1
    You might want to look into `UNPIVOT. Here is a nice little example: http://stackoverflow.com/questions/19055902/unpivot-with-column-name – Ralph May 15 '16 at 21:13
  • you should post expected result too ,good way to ask question to get better answers:https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ – TheGameiswar May 15 '16 at 21:27

1 Answers1

1

You are trying to unpivot, you can use T-SQL UNPIVOT covered here: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

Or, "An Alternative (Better?) Method to UNPIVOT (SQL Spackle)" by Dwain Camps found here: http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

Alan Burstein
  • 7,770
  • 1
  • 15
  • 18