views:

63

answers:

1

Hello

I got a table with one column eg: col1 and 5 records.

Eg: tablename: Table1

Col1
-------
aaaa
bbbb
cccc
dddd
eeee

I want to build a string based on col1 values as something below:

set @stringVariable = ''aaaa', 'bbbb', 'cccc', 'dddd', 'eeee''

How can this be done.

+4  A: 
DECLARE @stringVariable varchar (8000)
SET     @stringVariable = NULL  -- MUST be null to avoid leading comma.

SELECT
    @stringVariable     =  COALESCE (@stringVariable + '''' + ', ''', '') + Col1
FROM
    Table1

SET     @stringVariable = '''' + @stringVariable + ''''
Brock Adams
Thanks Brock that worked well.. just changed this to @stringVariable = COALESCE (@stringVariable + '''' + ',''', '') + Business_keyso that my result is as '035-7448-001-3854535','035-7448-001-3854536',.....
Nev_Rahd

related questions