 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 15th, 2006, 08:57 AM
|
|
Registered User
|
|
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Conditional selecting
I am trying to write a query that pulls a variety of columns from several tables, but I'm stuck on some conditional logic. I am passing in a variable and want to pull back all records from the primary table that contain that variable. At the same time, I am trying to pull back data from a second table that is related to the first (a key is stored in the primary table, and text tied to the key is stored in the second). There are two possible columns to return from the secondary table. If the first column in null, then I want to return the second column. I don't really know how to do this. Can someone point me in the right direction? Thanks.
|
|

August 15th, 2006, 09:04 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
You could look at the CASE statement in BOL. That might help you out.
Gert
|
|

August 15th, 2006, 02:29 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Also use THE COALEASE function or ISNULL function.
SELECT SomeColumn, ISNULL(ColA, ColB) FROM SomeTable WHERE ColC = @MyVar
If ColA is not null, ColA is returned.
If ColA is null, ColB is returned.
SELECT SomeColumn, COALESCE(ColA, ColB, '<no data>') FROM SomeTable WHERE ColC = @MyVar
If ColA is not null, ColA is returned.
If ColA is null, ColB is returned.
If ColB is null, "<no data>" is returned.
|
|

August 16th, 2006, 12:21 PM
|
|
Registered User
|
|
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Peso
Also use THE COALEASE function or ISNULL function.
SELECT SomeColumn, ISNULL(ColA, ColB) FROM SomeTable WHERE ColC = @MyVar
If ColA is not null, ColA is returned.
If ColA is null, ColB is returned.
SELECT SomeColumn, COALESCE(ColA, ColB, '<no data>') FROM SomeTable WHERE ColC = @MyVar
If ColA is not null, ColA is returned.
If ColA is null, ColB is returned.
If ColB is null, "<no data>" is returned.
|
|
|

August 16th, 2006, 12:22 PM
|
|
Registered User
|
|
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Peso
Also use THE COALEASE function or ISNULL function.
SELECT SomeColumn, ISNULL(ColA, ColB) FROM SomeTable WHERE ColC = @MyVar
If ColA is not null, ColA is returned.
If ColA is null, ColB is returned.
SELECT SomeColumn, COALESCE(ColA, ColB, '<no data>') FROM SomeTable WHERE ColC = @MyVar
If ColA is not null, ColA is returned.
If ColA is null, ColB is returned.
If ColB is null, "<no data>" is returned.
|
ISNULL was exactly what I was looking for! Thanks!
|
|
 |