Wrox Programmer Forums
|
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 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 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!
 
Old August 4th, 2009, 11:25 AM
Authorized User
 
Join Date: Nov 2005
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
 
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
 
Old August 4th, 2009, 02:13 PM
Friend of Wrox
 
Join Date: Jun 2008
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.
The Following User Says Thank You to Old Pedant For This Useful Post:
eastwest (August 6th, 2009)
 
Old August 5th, 2009, 12:48 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Old Pedant

You kill ...........
 
Old August 5th, 2009, 04:17 AM
Authorized User
 
Join Date: Nov 2005
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
 
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.
 
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!
 
Old August 6th, 2009, 05:19 PM
Friend of Wrox
 
Join Date: Jun 2008
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.






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





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