Concat/Join select result columns by char x

regae alternate 1 Reputation point
2021-10-12T11:01:30.207+00:00

set @alenzi = 'select ' + @IdentityColumns + ' from ' + @Tablename + ' where ' + @x
exec (@alenzi )

returns 1 to n columns. Means @IdentityColumns is a comma separated string. How can I concat the result with a char like '^'

For instance now I get 3 colums
Column Header: |A|B|C
Row 1: |a|b|c
Row 2: |d|e|f

but I want:
Column Header: |A
Row 1: |a^b^c
Row 2: |d^e^f

The solution has to work with old MSSql Server, too.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,675 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 118.9K Reputation points
    2021-10-12T14:13:21.533+00:00

    If it is not very old, then check this modification:

    set @alenzi = 'select concat_ws(''^'', ' + @IdentityColumns + ') as A from ' + . . .

    0 comments No comments

  2. EchoLiu-MSFT 14,591 Reputation points
    2021-10-13T06:08:16.407+00:00

    Hi @regae alternate ,

    Welcome to the microsoft TSQL Q&A forum!

    The following method is applicable to all supported versions SQL Server:

    set @sql = 'select ' + REPLACE(@IdentityColumns,',','^') + ' from ' + @Tablename + ' where ' + @x  
    

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. regae alternate 1 Reputation point
    2021-10-13T07:27:58.95+00:00

    Ok, thanks! concat_ws delivers the correct result. But it has to work with MsSql Server 2012. I tried to implement that with concat. Here is a simplified test to show the next issue:

    select REPLACE('PATIENTPK,PATNR', ',', ',''^'',') from patient

    returns

    PATIENTPK,'^',PATNR

    for each row of the patient table. If I copy and paste the return value into

    select concat(PATIENTPK,'^',PATNR) from patient

    The result ist correct. For instance

    1784^1

    Which means PATIENTPK and PATNR have been handled as column names, which is like should be.

    Now I tried to combine both and wraped the replace with concat:

    select concat(REPLACE('PATIENTPK,PATNR', ',', ',''^'',')) from patient

    That returns the error:
    The concat function requires 2 to 254 arguments.
    It seems like concat interprets the return value of replace as 1 argument. How can I change that?


  4. Viorel 118.9K Reputation points
    2021-10-13T09:31:14.403+00:00

    If you have to use REPLACE instead of CONCAT_WS, then try this:

    set @alenzi = 'select ' + replace(@IdentityColumns, ',', '+''^''+') + ' as A from ' + . . .

    It assumes that names do not contain ','.

    Maybe it is better to adjust the code that builds the @IdentityColumns variable.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.