Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Question

Posted on 2011-02-11
8
Medium Priority
?
247 Views
Last Modified: 2012-05-11
Say I have a table like this

Pcode      Val1      Val2
A             1           2
B             1           NULL
C             9           14

What SQL would I need to turn it into a single column table like this?

A1
A2
B1
C9
C14

0
Comment
Question by:Fulgent
[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
  • 6
8 Comments
 
LVL 60

Expert Comment

by:HainKurt
ID: 34871913
try this:

select a+val1 col from mytable
union
select a+val2 col from myTable

assuming they all are varchar
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 34871920
if not

select cast(a as varchar)+val1 col from mytable
union
select cast(a as varchar)+val2 col from myTable

0
 
LVL 60

Expert Comment

by:HainKurt
ID: 34871928
what do you want to do if val1 and val2 is same for row 1?
if you want 2 values then use union all

select cast(a as varchar)+val1 col from mytable
union all
select cast(a as varchar)+val2 col from myTable

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 60

Expert Comment

by:HainKurt
ID: 34871935
oops, you want to ignore nulls

select cast(a as varchar)+val1 col from mytable where val1 is not null
union all
select cast(a as varchar)+val2 col from myTable where val2 is not null

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 34871981
try:

select pcode + cast(val1 as nvarchar(10)) as mynewcolumn from mytesttable where val1 is not null
union
select pcode + cast(val2 as nvarchar(10)) as mynewcolumn from mytesttable where val2 is not null
0
 

Author Comment

by:Fulgent
ID: 34872369

Thanks for the replies, I forgot to mention that the number of columns is dynamic, can be anything up to 99 columns
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 34872527
up to 99 columns? give a sample for more than 2 columns...

something like this?

Pcode      Val1      Val2       Val3 ... Val99
A             1           2            NULL... 3
B             1           NULL      5......... 6
C             9           14          NULL... 1
0
 
LVL 60

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 34872552
create view v_myTable as
select pcode + cast(val1 as nvarchar(10)) as newcolumn from mytesttable where val1 is not null
union all
select pcode + cast(val2 as nvarchar(10)) from mytesttable where val2 is not null
union all
...
union all
select pcode + cast(val99 as nvarchar(10)) from mytesttable where val99 is not null

then use

select * from v_myTable
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

604 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