Solved

SQL Question

Posted on 2011-02-11
8
245 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 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 34871913
try this:

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

assuming they all are varchar
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Expert Comment

by:Huseyin KAHRAMAN
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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Accepted Solution

by:
Huseyin KAHRAMAN 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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