Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Who knows the answer fro this select statement

Status
Not open for further replies.

bhp

MIS
Jul 30, 2002
112
US
These are the valus in the table:
Siteid Link1 Link2 Link3
30861 NULL NULL NULL
30861 NULL NULL NULL
30861 NULL NULL NULL
30861 NULL NULL NULL
30861 NULL NULL NULL
30861 NULL NULL 542

How do I easily return just the siteid and whatever column holds a value, as it could be link1, link2 or link3
All I need to return in this case is 30861 and 542.
Thanks very much for anyone who can help!!

 
Code:
Select SiteId,
       Coalesce(Link1, Link2, Link3) As TheLink
From   Table
Where  Coalesce(Link1, Link2, Link3) is Not NULL
[code]



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi George
very clever I have never heard of the Coalesce command that is very neat thanks for your very fast response. Hope to help more folks myself in this forum, regards - James
 
I interpreted, as it could be link1, link2 [!]or[/!] link3 to mean, that there could only be 1. If I'm mistaken, then bhp will probably let us know.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
maybe as I also have a similar result and not sure whether Georges clever code will work:

Igroup link1 link2 link3
542 NULL NULL 6737
542 NULL NULL 30861
542 NULL NULL 7050
542 NULL 35121 NULL
542 NULL NULL NULL
542 NULL NULL NULL
542 NULL NULL NULL

In this case I just want to return the igroup and any numbers that appear in any of the link fields?
 
Code:
Select IGroup, Link1
From   Table 
Where  Link1 Is Not NULL

Union All

Select IGroup, Link2
From   Table 
Where  Link2 Is Not NULL

Union All

Select IGroup, Link3
From   Table 
Where  Link3 Is Not NULL

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
That last one will accomodate rows that have data in any combination of 'link' columns. For example, if one row had a link1 and a link3, but not a link2, this query will return the link1 and the link3.

As Denis suggests, a table redesign would improve this.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Coalesce? Works like Case? Geez.....
And why not use 'where link1 is not null and link2 is not null and link3 is not null' ?

"You cannot hold a torch to another man's path without brightening your own"
Best regards,
Bill
 
Coalesce Returns the first nonnull expression among its arguments.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top