Tuesday, June 9, 2009

Convert Multiple Row results into a variable , comma seperated into a single Row result

I wanted to display a set of row results in a single Row or a single variable by comma seperated values in a SQL query.

DATA from a select statement is

NAME
-----

Alpha
Beta
Gaama

Need result in the manner of

Desired Result

Alpha,Beta,Gaama


The can be done by

DECLARE @strText varchar(Max)
SELECT @strText = COALESCE(@strText + ',', '') + CONTACT_NAME FROM table
SELECT @strText

1 comment:

  1. FUTURE DAFS GOLD POINT PLAN BUY NSE GODREJCP ABOVE 1367.5 TARGET 1382-1396 SL 1347
    OUR FIRST TGT 1382 IN GODREJCP BUY CALL, ONCE BOOK PARTIAL PROFIT NOW 1377.9 AND TRAIL SL 1362
    Stock Trading Tips

    ReplyDelete

Express you views