p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

Go Back   p2p.wrox.com Forums > SQL Server > SQL Server 2000 > SQL Server 2000
I forgot my password
Register Now
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 computer programmers 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 programmers’ questions, win occasional prizes given to our best members, 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 June 22nd, 2003, 03:04 PM
Authorized User
Points: 342, Level: 6
Points: 342, Level: 6 Points: 342, Level: 6 Points: 342, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Yugoslavia.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default Case in Where Clause

Hi everybody
Can I use case statement in WHERE clause.
For example

DECLARE @VAR1 nvarchar(50)
DECLARE @VAR2 nvarchar(50)
SELECT * FROM ORDERS
WHERE CASE @VAR1
          WHEN 'Customers' THEN CustomerID = @VAR2
          WHEN 'Employee' THEN EmployeeID = @VAR2
          END

This is not working OK.
Can someone explain me the right way?
Thanks.
Best regards
Alex
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 22nd, 2003, 05:01 PM
Friend of Wrox
Points: 2,591, Level: 21
Points: 2,591, Level: 21 Points: 2,591, Level: 21 Points: 2,591, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In your situation, where you wish to select on two entirely different columns depending on a parameter, it is probably better to use an IF/ELSE construction, as:
Code:
IF @Var1='Customers' THEN
   SELECT * FROM Orders WHERE CustomerID=@Var2;
ELSE
   SELECT * FROM Orders WHERE EmployeeID=@Var2;
It's important to realize that CASE is an expression and not an executable statement.

The general form of the predicate of a WHERE clause is:
Code:
 expression operator expression
where operator is = or <> or > or < etc. The WHERE clause in your query does not follow this form, which is why you were having problems.

If you insist on using the CASE expression, then you can try a couple of ugly alternatives, like:
Code:
   SELECT * FROM Orders
    WHERE CustomerID=CASE @Var1 WHEN 'Customers' THEN @Var2 ELSE 0 END
       OR EmployeeID=CASE @Var1 WHEN 'Employees' THEN @Var2 ELSE 0 END
assuming that 0 is not a legal value for either 'CustomerID' or 'EmployeeID'. You could also try a nested CASE like:
Code:
   SELECT * FROM Orders
    WHERE 1 = 
      CASE @Var1
         WHEN 'Customers' THEN CASE WHEN CustomerID=@Var2 THEN 1 ELSE 0 END
         WHEN 'Employees' THEN CASE WHEN EmployeeID=@Var2 THEN 1 ELSE 0 END
      END
I think that the CASE expression doesn't work well in this situation and that the IF/ELSE is simpler and easier to understand.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old June 23rd, 2003, 09:31 AM
Authorized User
Points: 342, Level: 6
Points: 342, Level: 6 Points: 342, Level: 6 Points: 342, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Yugoslavia.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Very Much
Alex
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old August 6th, 2004, 03:43 PM
Registered User
Points: 2, Level: 1
Points: 2, Level: 1 Points: 2, Level: 1 Points: 2, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think this should work for you...

SELECT * FROM ORDERS
WHERE
(
  CASE
    WHEN @VAR1 = 'Customers' AND CustomerID = @VAR2 THEN 1
    WHEN @VAR1 = 'Employee' AND EmployeeID = @VAR2 THEN 1
    ELSE 0
  END
) = 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old September 18th, 2008, 09:57 AM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

or you could do this ...

SELECT *
FROM ORDERS
WHERE (@VAR1 = 'Customers' and CustomerID = @VAR2) OR
      (@VAR1 = 'Employee' and EmployeeID = @VAR2)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old September 18th, 2008, 03:56 PM
Friend of Wrox
Points: 4,922, Level: 29
Points: 4,922, Level: 29 Points: 4,922, Level: 29 Points: 4,922, Level: 29
Activity: 76%
Activity: 76% Activity: 76% Activity: 76%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,354
Thanks: 3
Thanked 72 Times in 71 Posts
Default

Jeff's first answer is best (though it doesn't handle an illegal value for @VAR1) and SBacon's answer is a close second, but Jeff's other answers and Craig's answer are all overkill.

Why not just:
Code:
DECLARE @VAR1 nvarchar(50)
DECLARE @VAR2 nvarchar(50)
SELECT * FROM ORDERS
WHERE @VAR2 = ( CASE @VAR1
                WHEN 'Customers' THEN CustomerID 
                WHEN 'Employee'  THEN EmployeeID 
                ELSE -1
                END )


The ELSE is in there to protect against an illegal value for @VAR1 of course.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old September 18th, 2008, 04:31 PM
Friend of Wrox
Points: 2,591, Level: 21
Points: 2,591, Level: 21 Points: 2,591, Level: 21 Points: 2,591, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Old Pedant
 ...Jeff's other answers ... are all overkill.
Well... I did say they were "...a couple of ugly alternatives..." ;)

(Any idea why we're responding to a 5 year old original post? :D )

Jeff Mason
je.mason@comcast.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old September 18th, 2008, 04:50 PM
Friend of Wrox
Points: 4,922, Level: 29
Points: 4,922, Level: 29 Points: 4,922, Level: 29 Points: 4,922, Level: 29
Activity: 76%
Activity: 76% Activity: 76% Activity: 76%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,354
Thanks: 3
Thanked 72 Times in 71 Posts
Default

> (Any idea why we're responding to a 5 year old original post?

Ummm...because I inanely didn't look at the original message date and because SBacon is a complete newbie and how the heck did he *find* this thread in the first place and... Well, maybe Google sent him this way??? But then why is he responding instead of asking? And... Ehhh, leave it at "because I inanely...".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old September 18th, 2008, 04:59 PM
Friend of Wrox
Points: 2,591, Level: 21
Points: 2,591, Level: 21 Points: 2,591, Level: 21 Points: 2,591, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Old Pedant
 > And...  Ehhh, leave it at "because I inanely...".
:D

Jeff Mason
je.mason@comcast.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CASE WHEN WHERE CLAUSE deontae45 SQL Server 2000 1 January 17th, 2007 12:31 PM
Case in Where Clause demiwolf SQL Server 2000 3 March 23rd, 2006 12:45 PM
Case Statement In Where Clause Logic dwj119 SQL Server 2000 3 December 2nd, 2005 10:10 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 04:54 AM



All times are GMT -4. The time now is 12:09 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 2010 Wiley Publishing, Inc