Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 15th, 2006, 08:57 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old August 15th, 2006, 09:04 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
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

Reply With Quote
  #3 (permalink)  
Old August 15th, 2006, 02:29 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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.


Reply With Quote
  #4 (permalink)  
Old August 16th, 2006, 12:21 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
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.


Reply With Quote
  #5 (permalink)  
Old August 16th, 2006, 12:22 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
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!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:32 PM.


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