Solved

How to list values from two columns in one column

Posted on 2010-09-21
10
246 Views
Last Modified: 2012-05-10
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
Comment
Question by:szadroga
10 Comments
 
LVL 1

Expert Comment

by:knackwursten
ID: 33728871
SELECT CONCAT(A,B) FROM table
Or with a space in between
SELECT CONCAT(A,' ',B) FROM table
0
 
LVL 1

Expert Comment

by:Dmapros
ID: 33728872
What are the field types for columns A and B?
0
 
LVL 8

Expert Comment

by:pdd1lan
ID: 33728884
do you use the concat function yet?

SELECT CONCAT(col1,col2) FROM table1
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33728912
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
 

Author Comment

by:szadroga
ID: 33728994
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33729023
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33729031
This question should not be in the SQL Server 2005 zone it seems.
0
 

Author Comment

by:szadroga
ID: 33730565
where do u suggest i post this question?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33730627
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
 

Author Comment

by:szadroga
ID: 33730667
no offense taken, sorry for the short response.  I appreciate all your help.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now