Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
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 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 May 21st, 2007, 11:07 AM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems using case statement in where clause

Hi Folks,

Is it possible to base case statement on one of the stored procedure parameters. Answer should be yes, but I can't seem to get my query, which uses case stmt in where clause, working:

I have a stored procedure with a following parameter:

@geography varchar,

my where clause looks like:

and comp.GeoFocus =
         case @geography
         when 'U.S.' then 'U.S.'
         when 'Asia' then 'Asia'
         when 'Europe' then "'Western Europe', 'Eastern Europe'"
         when 'Global' then 'Global'
         when 'ROW' then "'Non-U.S.', 'Latin America'"
         else comp.Geofocus
         end

It seems like it always execute else part here irrespective of value of @geography. Any ideas as to how could I get it working.
Reply With Quote
  #2 (permalink)  
Old May 21st, 2007, 05:25 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Wellington, FL , USA.
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I'm not sure you can do a case statement like that (meaning, combine a variable and column). For example, i tried the following:

DECLARE @var varchar(20), @val varchar(20)
SET @var = 'Global'

SELECT @val =
         case
         when @var = 'U.S.' then 'U.S.'
         when 'Asia' then 'Asia'
         when 'Europe' then "'Western Europe', 'Eastern Europe'"
         when @var = 'Global' then 'Global'
         when 'ROW' then "'Non-U.S.', 'Latin America'"
         else @var
         end

print @val

That didn't work. The only way to successfully get it to work was to do something like:

SELECT @val =
         case
         when @var = 'U.S.' then 'U.S.'
         when @var = 'Asia' then 'Asia'
         when @var = 'Europe' then "'Western Europe', 'Eastern Europe'"
         when @var = 'Global' then 'Global'
         when @var = 'ROW' then "'Non-U.S.', 'Latin America'"
         else @var
         end

OR

         case
         when 'U.S.' = 'U.S.' then 'U.S.'
         when 'Asia' then 'Asia'
         when 'Europe' then "'Western Europe', 'Eastern Europe'"
         when 'Global' = 'Global' then 'Global'
         when 'ROW' then "'Non-U.S.', 'Latin America'"
         else comp.Geofocus
         end

Hope this helps...

Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html
Reply With Quote
  #3 (permalink)  
Old May 23rd, 2007, 11:35 AM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You have to specify the length of your @geography parameter because if you don't, it defaults to a length of 1. So even if you pass 'U.S.' to your SP, the SP will only read it as 'U' and it won't match any of your criteria except for your else clause.

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
Reply With Quote
  #4 (permalink)  
Old May 28th, 2007, 05:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

vghiya,

Yes, looks like variable declaration is the culprit here as sshelper says. Did you miss the length of the variable by any chance?

_________________________
- Vijay G
Strive for 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 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 WHEN WHERE CLAUSE deontae45 SQL Server 2000 2 January 11th, 2011 10:03 AM
Case in Where Clause demiwolf SQL Server 2000 3 March 23rd, 2006 12:45 PM
Problems with GROUP BY and CASE statement karim SQL Server 2000 2 September 23rd, 2004 06:39 AM



All times are GMT -4. The time now is 07:11 AM.


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