Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

How to list values from two columns in one column

I am trying to combine the values from two columns (A and B) into one custom field (C).  I don't want anything fancy to take place.  Just need column C to list values of A followed by the values of B.  Just one long list.

Does anyone know the SQL function for this.  I thought there was a LIST() of sometype.
0
szadroga
Asked:
szadroga
1 Solution
 
knackwurstenCommented:
SELECT CONCAT(A,B) FROM table
Or with a space in between
SELECT CONCAT(A,' ',B) FROM table
0
 
DmaprosCommented:
What are the field types for columns A and B?
0
 
pdd1lanCommented:
do you use the concat function yet?

SELECT CONCAT(col1,col2) FROM table1
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
cyberkiwiCommented:
If you want each row's A and B to appear in C, then

select A + ' ' + B AS C
from tbl

If you want A to appear, then B to appear "A followed by the values of B" then (this assumes a and B are both varchar)

select C
from
(
select part='A', A
from tbl
union all
select part='B', B
from tbl
) X
order by part
0
 
szadrogaAuthor Commented:
The problem with the CONCAT() function is it combines the values of A and B into one cell.  I just want a list of the values in A (say 1-25) then followed by a list of values in B (26-35).  Is there any function that does this?  I am trying to avoid a UNION
0
 
cyberkiwiCommented:
Have you tried this?  It guarantees all values of A come before B.

select C
from
(
select part='A', A AS C
from tbl
union all
select part='B', B AS C
from tbl
) X
order by part

But if you really want a mix of A and B, where the sort order is determined by C itself, then

select C
from
(
select part='A', A AS C
from tbl
union all
select part='B', B AS C
from tbl
) X
order by C
0
 
cyberkiwiCommented:
This question should not be in the SQL Server 2005 zone it seems.
0
 
szadrogaAuthor Commented:
where do u suggest i post this question?
0
 
cyberkiwiCommented:
Sorry I don't mean to offend.
The question was posted in both SQL Server 2005 and MySQL.
I can give you answers for both, but from this statement above

"The problem with the CONCAT() function is it combines the values of A and B into one cell."

and CONCAT does not exist in SQL Server, I thought it was squarely in MySql.
I could be wrong.

The queries in http:#a33729023 are however Ansi compliant and apply equally to MySQL and SQL Server.
0
 
szadrogaAuthor Commented:
no offense taken, sorry for the short response.  I appreciate all your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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