Solved

SQL Question

Posted on 2011-02-11
8
234 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
  • 6
8 Comments
 
LVL 51

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 51

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 51

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

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 51

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 51

Accepted Solution

by:
HainKurt earned 500 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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

15 Experts available now in Live!

Get 1:1 Help Now