Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 question about select

Author  Topic 

shannara
Starting Member

2 Posts

Posted - 2005-12-05 : 09:09:33
I have a table in this structure:
rec_id | Name1 | Name2(optional) | Name3(optional)

now, is there any way in one command to get the following:
rec_id | Name(could be any of the three)

I mean: if i have:
======================================
1 | "one" | <null> | <null>
2 | "two" | "two-Two" | <null>
3 | "three" | "three-3" | "three-4"
4 | "four" | <null> | "four-4"
=====================================

i want to get the following:
============================
1 | "one"
2 | "two"
2 | "two-Two"
3 | "three"
3 | "three-3"
3 | "three-4"
4 | "four"
4 | "four-4"
==============================

any way this could be done?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-05 : 09:13:24
See if this helps you
http://www.sqlteam.com/item.asp?ItemID=2652

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

shannara
Starting Member

2 Posts

Posted - 2005-12-05 : 09:22:55
no.
I just want to do what I stated above.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-12-05 : 09:31:55
like this ?
create table #name
(
rec_id int,
Name1 varchar(10),
Name2 varchar(10),
Name3 varchar(10)
)
insert into #name (rec_id, Name1) select 1, 'one'
insert into #name (rec_id, Name1, Name2) select 2, 'two', 'two-Two'
insert into #name (rec_id, Name1, Name2, Name3) select 3, 'three', 'three-3', 'three-4'
insert into #name (rec_id, Name1, Name3) select 4, 'four', 'four-4'

select rec_id, Name1
from #name
where Name1 is not null
union all
select rec_id, Name2
from #name
where Name2 is not null
union all
select rec_id, Name3
from #name
where Name3 is not null
order by rec_id


-----------------
[KH]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-05 : 09:48:54
Well. Use Union All than Union for better performance

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-12-05 : 10:05:56
yes. Madhivanan you are right. thanks pointing that out.

-----------------
[KH]
Go to Top of Page
   

- Advertisement -