Solved

How to list values from two columns in one column

Posted on 2010-09-21
10
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using cfstoredproc to return query data 2 69
Where on a calculated field 1 36
Why is my $_POST not going to results page 10 68
MySQL_Development_Traininng.. 10 15
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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