Combine rows into a single table

  • I want to combine rows into a single row
    below is the data

    create table test
    (
    col1 varchar(5) null,
    col2 varchar(5) null
    )
    go
    insert into test values ('A', 'B')
    insert into test values ('C','D')
    insert into test values ('E','F')

    select

    * from test
    yields:

    col1col2

    A   B

    C   D

    E   F

    I want one

    row

    with values A B C D E F

    how can this be done?

  • jdswarrior - Wednesday, August 22, 2018 1:21 PM

    I want to combine rows into a single row
    below is the data

    create table test
    (
    col1 varchar(5) null,
    col2 varchar(5) null
    )
    go
    insert into test values ('A', 'B')
    insert into test values ('C','D')
    insert into test values ('E','F')

    select * from test
    yields:

    col1col2

    A   B

    C   D

    E   F

    I want one row with values A B C D E F

    how can this be done?

    Try this:
    DROP TABLE IF EXISTS #test;

    CREATE TABLE #test
    (
      col1 VARCHAR(5) NULL
    , col2 VARCHAR(5) NULL
    );
    GO

    INSERT #test
    VALUES
    (
      'A', 'B'
    )
    ,(
      'C', 'D'
    )
    ,(
      'E', 'F'
    );

    DECLARE @X VARCHAR(MAX) = '';

    SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
    FROM #test t;

    SELECT @X;


  • Have you tried anything so far? If not, consider the simple + operator and FOR XML PATH. Have a go, and if you get stuck please do reply with your attempt so that we can help you further.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Phil Parkin - Wednesday, August 22, 2018 1:35 PM

    jdswarrior - Wednesday, August 22, 2018 1:21 PM

    I want to combine rows into a single row
    below is the data

    create table test
    (
    col1 varchar(5) null,
    col2 varchar(5) null
    )
    go
    insert into test values ('A', 'B')
    insert into test values ('C','D')
    insert into test values ('E','F')

    select * from test
    yields:

    col1col2

    A   B

    C   D

    E   F

    I want one row with values A B C D E F

    how can this be done?

    Try this:
    DROP TABLE IF EXISTS #test;

    CREATE TABLE #test
    (
      col1 VARCHAR(5) NULL
    , col2 VARCHAR(5) NULL
    );
    GO

    INSERT #test
    VALUES
    (
      'A', 'B'
    )
    ,(
      'C', 'D'
    )
    ,(
      'E', 'F'
    );

    DECLARE @X VARCHAR(MAX) = '';

    SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
    FROM #test t;

    SELECT @X;

    yes that works - I should have been more specific - can I get output values in 6 separate columns?

  • jdswarrior - Wednesday, August 22, 2018 2:16 PM

    Phil Parkin - Wednesday, August 22, 2018 1:35 PM

    jdswarrior - Wednesday, August 22, 2018 1:21 PM

    I want to combine rows into a single row
    below is the data

    create table test
    (
    col1 varchar(5) null,
    col2 varchar(5) null
    )
    go
    insert into test values ('A', 'B')
    insert into test values ('C','D')
    insert into test values ('E','F')

    select * from test
    yields:

    col1col2

    A   B

    C   D

    E   F

    I want one row with values A B C D E F

    how can this be done?

    Try this:
    DROP TABLE IF EXISTS #test;

    CREATE TABLE #test
    (
      col1 VARCHAR(5) NULL
    , col2 VARCHAR(5) NULL
    );
    GO

    INSERT #test
    VALUES
    (
      'A', 'B'
    )
    ,(
      'C', 'D'
    )
    ,(
      'E', 'F'
    );

    DECLARE @X VARCHAR(MAX) = '';

    SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
    FROM #test t;

    SELECT @X;

    yes that works - I should have been more specific - can I get output values in 6 separate columns?

    I have to ask WHY?  It doesn't scale if you don't use dynamic SQL, and it won't scale well if you do.  If this is for a report, do this in your reporting software.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • jdswarrior - Wednesday, August 22, 2018 2:16 PM

    Phil Parkin - Wednesday, August 22, 2018 1:35 PM

    Try this:
    DROP TABLE IF EXISTS #test;

    CREATE TABLE #test
    (
      col1 VARCHAR(5) NULL
    , col2 VARCHAR(5) NULL
    );
    GO

    INSERT #test
    VALUES
    (
      'A', 'B'
    )
    ,(
      'C', 'D'
    )
    ,(
      'E', 'F'
    );

    DECLARE @X VARCHAR(MAX) = '';

    SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
    FROM #test t;

    SELECT @X;

    yes that works - I should have been more specific - can I get output values in 6 separate columns?

    I'm not saying this is a good solution, but this adjustment to Phil's solution will make them into columns...

    DROP TABLE IF EXISTS #test;

    CREATE TABLE #test
    (
    col1 VARCHAR(5) NULL
    , col2 VARCHAR(5) NULL
    );
    GO

    INSERT #test
    VALUES
    ('A', 'B'),('C', 'D'),('E', 'F');

    DECLARE @X VARCHAR(MAX) = '';

    SELECT @X =
      'select ' + STUFF(
      (SELECT DISTINCT ', ''' + t.[col1] + ''' , ''' + t.[col2] + '''' , ' '
       FROM #test t FOR XML PATH(''), TYPE
      ).value('.', 'nvarchar(max)'), 1, 1, '');

    EXEC (@x);

  • work well - thx - i have an alternative solution already developed - wanted to compare the two and see what works best in powershell - we are looping thru a list of over 200 servers in a text file and want to run a SQLCMD and get a list of linked servers and data_source names from sys.servers

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply