 |
| 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
|
|
|
|

August 4th, 2009, 10:20 AM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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!
|
|

August 4th, 2009, 11:25 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 41
Thanks: 6
Thanked 1 Time in 1 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
__________________
Rob Carter
|
|

August 4th, 2009, 02:08 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thanks a lot!
Quote:
Originally Posted by RobCarter
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
|
|
|

August 4th, 2009, 02:13 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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:
|
|
|

August 5th, 2009, 12:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
Old Pedant
You kill ...........
|
|

August 5th, 2009, 04:17 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 41
Thanks: 6
Thanked 1 Time in 1 Post
|
|
Pedant by name, pedant by nature
Totally forgot about isnull().
__________________
Rob Carter
|
|

August 6th, 2009, 03:17 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank You!
Quote:
Originally Posted by Old Pedant
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.
|
|
|

August 6th, 2009, 03:17 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank You!
|
|

August 6th, 2009, 05:19 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Quote:
Originally Posted by RobCarter
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.
|
|
 |