convert rows into column using sql query

Posted by Venkatesh under Sql Server on 8/23/2013 | Points: 10 | Views : 2055 | Status : [Member] | Replies : 5
Hi,

How to convert rows to into columns in sql query. it is like dynamic

ex: in my dataset getting the columns like
abc
xyz
123

but i need these into abc xyz 123 .If i pass query to another table those rows convert to columns. How can i do this in sql query.

Thanks,
Venkatesh.P




Responses

Posted by: Satyapriyanayak on: 8/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.aspdotnet-suresh.com/2012/09/how-to-convert-rows-to-columns-in-sql.html
http://forums.asp.net/t/1851916.aspx/1

If this post helps you mark it as answer
Thanks

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 8/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.aspdotnet-suresh.com/2012/09/how-to-convert-rows-to-columns-in-sql.html
http://forums.asp.net/t/1851916.aspx/1

If this post helps you mark it as answer
Thanks

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
declare @pvtColumns varchar(300)='', @sql varchar(max)=''
select @pvtcolumns = ','+ columNameinTab FROM tablename
set @pvtcolumns =stuff(@pvtcolumns,1,1,'')
set @sql = 'select * from tablename
pivot ((count(id) for columNameinTab IN (' + @pvtcolumns+'))pvt'
exec(@sql)
tablename is your table
columns are columNameinTab, id


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
sample example on dynamic pivoting
http://sqlmag.com/t-sql/pivoting-dynamic-way

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Venkatesh,
Have you found the solution with the above replies...
If NOT, let us know the exact requirement which you need....
Do you want to convert single column values into multiple columns or something else?

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response