0

I have the following Sql Server 2016 SELECT statement that returns only 1 row:

SELECT TOP 1 * FROM tempdb.dbo.IMTD

How can I concatenate the values as a comma delimited string? NOTE: the column names of this temporary table are unknown as they can variate.

Thank you.

2
  • How are you executing this query? SSMS? Powershell? Your own application? Commented Oct 18, 2016 at 18:27
  • @alroc I am using SSMS Commented Oct 18, 2016 at 18:51

4 Answers 4

1

Something like this perhaps:

-- Sample data
DECLARE @someTable TABLE (SomeID int identity, SomeTxt varchar(100));
INSERT @someTable VALUES ('row1'),('row2'),('row3');

-- Solution
SELECT ConcatinatedString = 
STUFF
((
  SELECT ','+SomeTxt
  FROM @someTable
  FOR XML PATH(''), TYPE
).value('.','varchar(100)'),1,1,'');
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please specify what is "SomeTxt", I don't have any information about column names in the temporary table?
0

You can use Dynamic query as below:

DECLARE @COLS VARCHAR(MAX) = ''

SELECT @COLS = @COLS + ',' + COLUMN_NAME
FROM tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '#table[_]%' -- Dynamic Table (here, Temporary table)

DECLARE @COLNAMES VARCHAR(MAX) = REPLACE(STUFF(@COLS, 1, 1, ''), ',', '+ '','' +')

Declare @cmd varchar(max) = 'Select ' + @COLNAMES + ' as CSVCol from #table'
-- will generate
-- Select Column1+ ',' +Column2+ ',' +Column3 as CSVCol from #table
EXEC (@cmd)

6 Comments

Weird, @COLUMNNAMES is truncated to 256 characters (but the code should work correctly). Any suggestion? Thanks!
I guess it might be due to the datatype. Can you try with nvarchar by replacing varchar?
Yes, I did: DECLARE @COLNAMES NVARCHAR(MAX) = REPLACE(STUFF(@COLS, 1, 1, ''), ',', '+ '','' +')
What length you are getting for SELECT LEN(@COLS) ?
SELECT len(@COLS) = 171; SELECT LEN(@COLNAMES)=346
|
0

Another solution you can try is this.

SELECT LTRIM(RTRIM(<ColumnName1>)) + ',',
       LTRIM(RTRIM(<ColumnName2>)) + ',',
       ...
       LTRIM(RTRIM(<ColumnNamen>)) + ','
FROM tempdb.dbo.IMTD

If you only want one row keep that top 1 In there like

    SELECT TOP 1
       LTRIM(RTRIM(<ColumnName1>)) + ',',
       LTRIM(RTRIM(<ColumnName2>)) + ',',
       ...
       LTRIM(RTRIM(<ColumnNamen>)) + ','
FROM tempdb.dbo.IMTD

The LTRIM and RTRIM will remove any white space and this should allow you to copy and paste the result set anywhere you may need it. You will need to do this for each columnname.

2 Comments

I tried both qureies, I get: Incorrect syntax near '*'.
@M.R. sorry I forgot you have to specify each column name like in the edit above.
0

You can use the query below to get the column names from your temp table.

 DECLARE @ColumnNames NVARCHAR(MAX)

 SELECT 
    @ColumnNames= COALESCE(@ColumnNames +',','')+COLUMN_NAME  
 FROM 
    TempDB.INFORMATION_SCHEMA.COLUMNS 
 WHERE 
    TABLE_NAME = '#TempTableName'

1 Comment

I see... I thought I can avoid this, it becomes complicated...Thank you for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.