Tuesday, February 14, 2012

sql rows to columns comma separated values

We can convert rows to column using following querys.

 

in case we have more rows then we can use following query.( only 500000000 characters)

 

 

--Sql rows to column

--only 500000000 characters--If you want more rows,then you can use the following query

 

select SUBSTRING((SELECT(', ' + CAST(Columname AS varchar(1000)) ) FROM

tablename FOR XML PATH('')),3,500000000)

 

in case we have less rows then we can use following query.( only 8000 characters)

 

--If you want less rows , then you can use the following query--only8000 characters

DECLARE @retstr varchar(MAX) SELECT @retstr =

COALESCE(@retstr + ',','') + [Columname] FROM tablename

select @retstr

 Happy coding......

No comments: