Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 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 4th, 2009, 10:20 AM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default Help with Case Statement

Hello,

I have the following code in my stored procedure:


Code:
[CTTitle] =(SELECT TOP 1 TITLE FROM mms.dbo.vwcommitteetermmembers WHERE ID =
      (SELECT pl.PresID FROM #PresidentList pl 
       WHERE pl.ClubCTID = (SELECT TOP 1 ID FROM mms.dbo.vwcommitteeterms ct WHERE ct.lccompanyid = d.donorcompanyId))
        AND YEAR(ENDDATE) =
                (CASE
                  WHEN MONTH(GETDATE()) IN (7,8,9,10,11,12) THEN YEAR(GETDATE()) +1
                   ELSE YEAR(GETDATE())
                END)),
  
[CTTitle1] =(SELECT  TOP 1 TITLE FROM mms.dbo.vwcommitteetermmembers WHERE MemberID  = od.ShipToID and rank=405 
          AND YEAR(ENDDATE) =
             (CASE
              WHEN MONTH(GETDATE()) IN (7,8,9,10,11,12) THEN YEAR(GETDATE()) +1
               ELSE YEAR(GETDATE())
                END))
I want to achieve if [CTTitle] is null then I want to display [CTTitle1] else [CTTitle].

Please anybody let me know how to accomplish this.

Thanks!
Reply With Quote
  #2 (permalink)  
Old August 4th, 2009, 11:25 AM
Authorized User
 
Join Date: Nov 2005
Location: Bromsgrove, United Kingdom.
Posts: 41
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to RobCarter
Default

Hi

How about trying:

Code:
 
case 
when [cttitle] is null then [cttitle1]
else [cttitle]
end
As long as you use the same criteria language in your case statement as you would in a view (i.e. [cttitle] is null rather than [cttitle] = null) then you should be fine

This also would apply if you want to compare 2 criteria on the values of [cttitle]:
e.g.

Code:
case 
when [cttitle] >1 and [cttitle] is <10 then [cttitle1] 
else [cttitle]
end
hope this helps

Rob
__________________
Rob Carter
Reply With Quote
  #3 (permalink)  
Old August 4th, 2009, 02:08 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks a lot!

Quote:
Originally Posted by RobCarter View Post
Hi

How about trying:

Code:
 
case 
when [cttitle] is null then [cttitle1]
else [cttitle]
end
As long as you use the same criteria language in your case statement as you would in a view (i.e. [cttitle] is null rather than [cttitle] = null) then you should be fine

This also would apply if you want to compare 2 criteria on the values of [cttitle]:
e.g.

Code:
case 
when [cttitle] >1 and [cttitle] is <10 then [cttitle1] 
else [cttitle]
end
hope this helps

Rob
Reply With Quote
  #4 (permalink)  
Old August 4th, 2009, 02:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...there is a MUCH easier solution, if you are using SQL Server:
Code:
SELECT ISNULL( cttitle, cttitle1 ) AS thetitle
The ISNULL( ) function in SQL Server does *exactly* what you asked.
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
eastwest (August 6th, 2009)
  #5 (permalink)  
Old August 5th, 2009, 12:48 AM
Friend of Wrox
Points: 2,101, Level: 18
Points: 2,101, Level: 18 Points: 2,101, Level: 18 Points: 2,101, Level: 18
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: , , .
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Old Pedant

You kill ...........
Reply With Quote
  #6 (permalink)  
Old August 5th, 2009, 04:17 AM
Authorized User
 
Join Date: Nov 2005
Location: Bromsgrove, United Kingdom.
Posts: 41
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to RobCarter
Default

Pedant by name, pedant by nature

Totally forgot about isnull().
__________________
Rob Carter
Reply With Quote
  #7 (permalink)  
Old August 6th, 2009, 03:17 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank You!

Quote:
Originally Posted by Old Pedant View Post
Ummm...there is a MUCH easier solution, if you are using SQL Server:
Code:
SELECT ISNULL( cttitle, cttitle1 ) AS thetitle
The ISNULL( ) function in SQL Server does *exactly* what you asked.
Reply With Quote
  #8 (permalink)  
Old August 6th, 2009, 03:17 PM
Registered User
 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank You!
Reply With Quote
  #9 (permalink)  
Old August 6th, 2009, 05:19 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
Originally Posted by RobCarter View Post
Pedant by name, pedant by nature
Thanks to Philip M in codingforums.com for this quote:
“Fools act on imagination without knowledge, pedants act on knowledge without imagination”
-- Alfred North Whitehead (British Mathematician and Philosopher, 1861-1947)


My wife says that describes me to perfection.

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
Case statement cole SQL Language 3 May 8th, 2005 03:02 PM
case statement Hudson40 Access VBA 1 February 11th, 2005 11:31 AM
Using A CASE Statement fastcorvette Access 5 December 24th, 2003 01:39 PM
HELP - IF ELSE & CASE statement savoym SQL Language 3 September 12th, 2003 06:35 AM
case statement jakeone Beginning PHP 10 August 19th, 2003 03:03 PM



All times are GMT -4. The time now is 02:44 PM.


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