Wrox Programmer Forums
| 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 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
  #1 (permalink)  
Old January 17th, 2007, 11:49 AM
Authorized User
 
Join Date: Sep 2006
Location: Montgomery, Alabama, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default CASE WHEN WHERE CLAUSE

Hi everybody, I need some help. I'm trying to get a solution from the following code: The WHERE clause is where I'm having problems. I'm trying to combine two statements in the where clause if I run them seprate I get the answer I'm looking for but I want to combine the two to get one answer. I think I need a CASE WHEN in the WHERE Clause. Can anyone help me??

SELECT SUM(mPremium) as 'Total Gross Prem', SUM(mPremium)+
SUM((Case When mPremium < 0 Then mPremium Else 0 End)) as 'Total Net Prem',
SUM((Case When mPremium < 0 Then mPremium Else 0 End))as 'Total Return Prem',
(SUM(mPremium)+ SUM((Case When mPremium < 0 Then mPremium Else 0 End)))* .06 as 'Total Tax Due'
From tblPolicy
Where (dtSubmitDate >= '1/1/2005' and dtSubmitDate <= '12/31/2005'and dtPolicyIncepDate < '1/1/2005'and
vchTransactionType <> 'NEW POLICY' and vchTransactionType <> 'Renewal')
and vchSLBLicNo = 'A172757'
and
Case When (dtPolicyIncepDate >='1/1/2005' and
dtPolicyIncepDate <= '12/31/2005' and vchSLBLicNO = 'A172757') end

Alexander Nelson
Programmer
__________________
Alexander Nelson
Programmer
  #2 (permalink)  
Old January 17th, 2007, 12:31 PM
Authorized User
 
Join Date: Dec 2006
Location: Lake Wylie, SC, USA.
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The CASE statement is incomplete. THEN is required to determine the result of the case.
(i.e. SELECT * FROM table WHERE table_id = CASE WHEN (1 = 1) THEN 1 ELSE 0 END)

What are the two original statements?


Adam Gossage
Lake Wylie, SC, USA
  #3 (permalink)  
Old January 11th, 2011, 10:03 AM
Registered User
Points: 9, Level: 1
Points: 9, Level: 1 Points: 9, Level: 1 Points: 9, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add a Case expression to a Where clause

Here are a couple of examples on how to add a Case expression to a where clause. For more complex case expressions or boolean operations, you could use a scalar function and just return a value:

How to Add a Case expression to a Where clause using a function


Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Statement In Where Clause Logic dwj119 SQL Server 2000 4 October 27th, 2011 03:14 PM
Case in Where Clause acko SQL Server 2000 12 September 12th, 2011 01:43 PM
Case in Where Clause demiwolf SQL Server 2000 3 March 23rd, 2006 12:45 PM
case clause in query mateenmohd SQL Server 2000 2 February 22nd, 2005 05:49 AM
case clause in Access mateenmohd Access 19 May 6th, 2004 03:54 AM





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