Wrox Programmer Forums
|
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
 
Old August 15th, 2006, 08:57 AM
Registered User
 
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old August 15th, 2006, 09:04 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

You could look at the CASE statement in BOL. That might help you out.

Gert

 
Old August 15th, 2006, 02:29 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.


 
Old August 16th, 2006, 12:21 PM
Registered User
 
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.


 
Old August 16th, 2006, 12:22 PM
Registered User
 
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional transformation vvenk XSLT 3 July 10th, 2008 09:09 AM
conditional IF roy_mm Reporting Services 0 October 30th, 2006 07:52 AM
Datagrid Conditional ~Bean~ ASP.NET 1.0 and 1.1 Basics 3 July 27th, 2005 12:40 AM
Conditional Expressions jquinn BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 October 4th, 2004 09:36 AM
C# Conditional statements sporkman43 General .NET 2 July 23rd, 2004 09:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.