• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 51
  • Last Modified:

Concatenate 2 columns into one but order by rows in SQL Query

I have 2 columns in SQL table.

Combine or concatenate these 2 columns and the output should be in single column but in row order. See attached.

Please help.

Thank you for your help.
Concatenate2ColumnsIntoOneButInRowO.xlsx
0
Queennie L
Asked:
Queennie L
  • 8
  • 7
1 Solution
 
Bill PrewCommented:
I might be missing something, but this looks like it would be as below (I assumed you wanted a space between each concatenated value):

SELECT UniqueID, Column1, Column2, Column1+' '+Column2 AS Column3 FROM Table1

Open in new window



»bp
0
 
Queennie LAuthor Commented:
@Bill:

I would like the column3 to be in row order.

thank you.
0
 
Bill PrewCommented:
I would like the column3 to be in row order

I don't know what that means, can you clarify further please?


»bp
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Bill PrewCommented:
Do you mean like this?

SELECT UniqueID, Column1, Column2, Column1+CHAR(10)+Column2 AS Column3 FROM Table1

Open in new window



»bp
0
 
Queennie LAuthor Commented:
@Bill:

Please see attached example.

Column3 should be:
AAAA
BBBB

Not like this: AAAA BBBB

Not in one row but 2 rows in one column.

Thank you.
0
 
Bill PrewCommented:
The output from my second query looks exactly like your Excel sample.

sshot-388.png

»bp
0
 
Queennie LAuthor Commented:
@Bill:

I will try it again.

Thank you.
0
 
Queennie LAuthor Commented:
@BILL:

I tried it but still one single line.

Thank you.
0
 
Bill PrewCommented:
Here is my test and the results, perhaps if you shared your query here.

EE29078966, Sql Server - rextester


»bp
0
 
Queennie LAuthor Commented:
@Bill:

In my query, still showing in one line. I know CHAR(10) is new line.

I don't know why my query is not populating correctly.

Thank you.
0
 
Bill PrewCommented:
How are you executing your query, in SSMS, or something else?


»bp
0
 
Bill PrewCommented:
If in SSMS then it doesn't look like it's grid presentation of the results honors the line fee or carriage return.  So don't think you can do it there.

You may have to take a different approach, or if the query is ultimately to be used in some other presentation tool (HTML, etc) then do the formatting there.


»bp
0
 
Queennie LAuthor Commented:
@Bill:

This is just a simple tsql.

Thank you.
0
 
Bill PrewCommented:
I don't think you can get the effect you want in SSMS.


»bp
0
 
larryhSr. Software EngineerCommented:
How are you wanting to use the output of your query?  If you just want to be able to read it that's one thing but if you need to be able to cut/paste it into some other program that may be something else entirely.  There is output to text, grid and file in SSMS.  If we had more info on how you want to use the information that would help us help you better.
0
 
Queennie LAuthor Commented:
Thank you again for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now