Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate all columns in a select with SQL Server

I need my select to have a pattern like this:

 SELECT '<text> ' + tbl.* + ' </text>' FROM table tbl;

The ideal solution would have all the columns separated by a comma in order to have that output:

SQL result for Table 1 with two columns:

'<text>col1, col2</text>'

SQL result for Table 2 with three columns:

'<text>col1, col2, col3</text>' 

I tried to use the CONCAT(...) function like this:

SELECT CONCAT('<text>', tbl.*, '</text>')
FROM table2 tbl

But I understand it is not so simple because the variable number of columns.

Is there any simple solution to address that problem?

I am using SQL Server 2008 R2.

like image 779
рüффп Avatar asked Dec 07 '12 10:12

рüффп


People also ask

How do I concatenate multiple columns in SQL Server?

To concatenate more than 2 fields with SQL, you can use CONCAT() or CONCAT_WS() function.

Can you concatenate columns in SQL?

In SQL, you can also concatenate numerical data from the table in the same way as we concatenate strings. The CONCAT function can also be used to join numeric values.


2 Answers

Any number of columns for a given tablename; If you need column names wrapped with <text>

DECLARE @s VARCHAR(500)

SELECT @s =  ISNULL(@s+', ','') + c.name   
FROM  sys.all_columns c join sys.tables  t 
            ON  c.object_id = t.object_id
WHERE t.name = 'YourTableName'

SELECT '<text>' + @s + '</text>'

SQL Fiddle Example here

-- RESULTS 
<text>col1, col2, col3,...</text>

If you need select query result set wrapped with <text> then;

SELECT @S =  ISNULL( @S+ ')' +'+'',''+ ','') + 'convert(varchar(50), ' + c.name    FROM 
       sys.all_columns c join sys.tables  t 
       ON  c.object_id = t.object_id
WHERE t.name = 'YourTableName'


EXEC( 'SELECT ''<text>''+' + @s + ')+' + '''</text>'' FROM YourTableName')

SQL Fiddle Example here

--RESULTS
<text>c1r1,c2r1,c3r1,...</text>
<text>c1r2,c2r2,c3r2,...</text>
<text>c1r3,c2r3,c3r3,...</text>
like image 189
Kaf Avatar answered Oct 06 '22 00:10

Kaf


SQL Fiddle

MS SQL Server 2008 Schema Setup:

create table YourTable
(
  ID int identity primary key,
  Name varchar(50),
)

insert into YourTable values
('Name 1'),
('Name 2'),
('Name 3'),
('Name 4'),
('Name 5')

Query 1:

select (
       select (
              select ', '+T2.N.value('./text()[1]',  'varchar(max)')
              from (
                   select T.*
                   for xml path(''), type
                   ) as T1(N)
                cross apply T1.N.nodes('/*') as T2(N)
              for xml path(''), type
              ).value('substring(./text()[1], 3)',  'varchar(max)')
       for xml path('text'), type
       )
from YourTable as T

Results:

|               COLUMN_0 |
--------------------------
| <text>1, Name 1</text> |
| <text>2, Name 2</text> |
| <text>3, Name 3</text> |
| <text>4, Name 4</text> |
| <text>5, Name 5</text> |
like image 26
Mikael Eriksson Avatar answered Oct 05 '22 23:10

Mikael Eriksson