473,699 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with case statement in where clause

8 New Member
I was wondering if someone would be able to tell me what is wrong with my code here:

SELECT COUNT(*)
FROM CALL
WHERE CALL.DESCRIPTIO N =
CASE
WHEN @Param1 = 'In' THEN 'In'
WHEN @Param1 = 'Out' THEN 'Out'
END

I am getting an error that parameter is correct.

And just in case there is any confusion, I am taking in 1 parameter that is either going to be 'In' or 'Out'. Depending on the param, I want to set Call.Descriptio n to either 'In' or 'Out'.

Thanks in advance!
Aug 6 '08 #1
15 9390
ck9663
2,878 Recognized Expert Specialist
I was wondering if someone would be able to tell me what is wrong with my code here:

SELECT COUNT(*)
FROM CALL
WHERE CALL.DESCRIPTIO N =
CASE
WHEN @Param1 = 'In' THEN 'In'
WHEN @Param1 = 'Out' THEN 'Out'
END

I am getting an error that parameter is correct.

And just in case there is any confusion, I am taking in 1 parameter that is either going to be 'In' or 'Out'. Depending on the param, I want to set Call.Descriptio n to either 'In' or 'Out'.

Thanks in advance!
What error are you getting?

You can actually just do this:

Expand|Select|Wrap|Line Numbers
  1. SELECT     COUNT(*) 
  2. FROM         CALL
  3. WHERE     CALL.DESCRIPTION =  @Param1

-- CK
Aug 6 '08 #2
shlo
8 New Member
What error are you getting?

You can actually just do this:

Expand|Select|Wrap|Line Numbers
  1. SELECT     COUNT(*) 
  2. FROM         CALL
  3. WHERE     CALL.DESCRIPTION =  @Param1

-- CK
Sorry, I forgot my else statement in my sample code:

What I'm trying to do is set get the count of calls based on the parameter. The parameter determines whether call description is equal to 'In', 'Out', or 'Any'. If any, I want to view calls with description of 'In' or 'Out'.

So,
if @Param1 = 'In', then call.descriptio n = 'In'
if @Param1 = 'Out', then call.descriptio n = 'Out'
if @Param1 = 'Any' then call.descriptio n = 'In' OR call.descriptio n = 'Out'

This is why I was using a case statement. Any suggestions would be greatly appreciated!! And I was getting an error that 'Parameter is Incorrect'.
Aug 6 '08 #3
ck9663
2,878 Recognized Expert Specialist
So if I understand it right:

if @param = 'In' then you want all rows with CALL.DESCRIPTIO N = In'.
if @param = 'Out' then you want all rows with CALL.DESCRIPTIO N = Out'.
if @param = 'Any' then you want all rows, both CALL.DESCRIPTIO N = 'In' or CALL.DESCRIPTIO N = 'Out'

If my assumption is wrong, don't read the following, otherwise try the following:

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*)
  2. FROM CALL
  3. WHERE (CALL.DESCRIPTION =
  4. CASE
  5. WHEN @Param1 = 'In' THEN 'In'
  6. WHEN @Param1 = 'Out' THEN 'Out'
  7. END) or  (@Param1 = 'Any' and CALL.DESCRIPTION in ('In','Out'))
  8.  

-- CK
Aug 7 '08 #4
shlo
8 New Member
So if I understand it right:

if @param = 'In' then you want all rows with CALL.DESCRIPTIO N = In'.
if @param = 'Out' then you want all rows with CALL.DESCRIPTIO N = Out'.
if @param = 'Any' then you want all rows, both CALL.DESCRIPTIO N = 'In' or CALL.DESCRIPTIO N = 'Out'

If my assumption is wrong, don't read the following, otherwise try the following:

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*)
  2. FROM CALL
  3. WHERE (CALL.DESCRIPTION =
  4. CASE
  5. WHEN @Param1 = 'In' THEN 'In'
  6. WHEN @Param1 = 'Out' THEN 'Out'
  7. END) or  (@Param1 = 'Any' and CALL.DESCRIPTION in ('In','Out'))
  8.  

-- CK
That worked perfectly. Thanks so much for your help!
Aug 7 '08 #5
shlo
8 New Member
Another question about case statements, now in ORDER BY.
Here is my query:

SELECT START_DATE, CALL_ID
FROM CALL

ORDER BY
CASE WHEN @OrderBy ='A' THEN START_DATE, CALL_ID
WHEN @OrderBy ='D' THEN START_DATE DESC, CALL_ID
WHEN @OrderBy ='R' THEN CALL_ID, START_DATE DESC
END

I looked up a bunch of articles and they seem to give this syntax. But, I get a SQL Execution Error with a message: Must declare the scalar variable @OrderBy. I am not doing this in a context of a stored procedure, so what am I missing?

Thanks in advance...
Aug 12 '08 #6
ck9663
2,878 Recognized Expert Specialist
Anything that starts with "@" is a variable. If you mean column, remove that symbol.

-- CK
Aug 12 '08 #7
shlo
8 New Member
Anything that starts with "@" is a variable. If you mean column, remove that symbol.

-- CK
I meant it to be a parameter that dynamically determines the ORDER BY. Since this is not a stored procedure, however, I don't know how to define the parameter before run time. Does this make sense?
Aug 12 '08 #8
ck9663
2,878 Recognized Expert Specialist
Where are you running it? SQL Analyzer?

The query needs to know the value of those variable before it runs. You have to declare it and store a value to it before your query can run.

-- CK
Aug 12 '08 #9
shlo
8 New Member
Where are you running it? SQL Analyzer?

The query needs to know the value of those variable before it runs. You have to declare it and store a value to it before your query can run.

-- CK
I'm testing this in SQL Server 2005, Management Studio.

Correct me if I'm wrong, but when you're just running a query in SS, you can just type the query, press 'Execute' and enter the parameters at that time, no? I'm confused because when I have a query with parameters in the SELECT or WHERE clause, it works perfectly fine.

Expand|Select|Wrap|Line Numbers
  1. SELECT     *
  2. FROM CALL
  3. WHERE CALL.DESCRIPTION =  @Param1
However, when I have the parameter in the ORDER BY, I'm being asked to define the variable. The exact error message: Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "@orderBy".

Expand|Select|Wrap|Line Numbers
  1. SELECT START_DATE, CALL_ID
  2. FROM CALL
  3. ORDER BY CASE WHEN @orderBy = 'A' THEN START_DATE, CALL_ID END
I guess the real question is this second bit of code correct? If so, what is causing the error message of 'Must declare the scalar variable 'OrderBy''? All the examples I've seen only show dynamic ORDER BY in stored procedures, so I don't know.

Thanks again.
Aug 12 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5777
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also stores other fields from our dynamic forms. The field is called 'FormItemAnswer' and stores text, integer, date, float, etc. Anything the user can type into one of our web forms. The query looks like, select distinct from...
2
188938
by: Largo SQL Tools | last post by:
Can anyone tell me if it's possible to use a Case statement in a Where clause, and if so, the proper syntax? J.R. Largo SQL Tools The Finest Collection of SQL Tools Available http://www.largosqltools.com
1
7132
by: mirth | last post by:
I would like to update a decimal column in a temporary table based on a set of Glcodes from another table. I search for a set of codes and then want to sum the value for each row matching the Glcodes. The problem is I keep getting multiple rows returned errors. "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been...
4
28820
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on this with relation to the following example (is there a way to acheive this without re-stating the...
4
1604
by: ree32 | last post by:
I know when you are using group by functions you have to include all the columns in the GROUP BY clause. But what I am having problems when using a case statement to determine whether to sum of not a column. eg. SELECT Country, Case WHEN Age<15 THEN Sum(Income) ELSE NULL END AS YouthIncome, Case WHEN Age>65 THEN Sum(Income) ELSE NULL END AS PensionIncome FROM WORLDTABLE
8
2478
by: Jeff Gilbert | last post by:
Hello all. I'd appreciate some help with this one: First the DDL: CREATE TABLE ( NOT NULL , NULL , NOT NULL , (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL CONSTRAINT DEFAULT
7
6220
by: mandible | last post by:
Hello I'm trying to have some control on how my data is ordered depending on an input parameter my question is in a stored procedure how can I do something like this at the end of my statement. pOrder as input value where pOrder can be 1 or 0
1
5005
by: priyanka2203 | last post by:
Hi guys, I have a doubt regarding the CASE statement. It might sound silly, but me being new to DB2, it is kind of a genuine doubt. Try helping me with this.. When we use a case statement (simple-case-statement-when-clause), in this - the value of the expression prior to the first WHEN keyword is tested for equality with the value of each expression that follows the WHEN keyword. Right? Then if the search condition is true, the THEN...
7
2988
by: pbd22 | last post by:
Hi. I really need some advice on fine-tuning a stored procedure that is the meat of the search logic on my site. Customers are allowed to save searches, which dumps the search logic in a table called SavedSearches for later access to the search. My problem started with the ORDERBY condition used for zipcode searches. The condition did something like: "order by CASE WHEN userID=67 THEN 1 WHEN userID=103 THEN 2 WHEN
5
15504
by: Sascha.Moellering | last post by:
Hi, I receive the error code SQL0338N if I try to compile this statement (part of the statement): .... left outer join lateral (SELECT * FROM LIZSYSABA.VWZL0359TBS WHERE tbs_name = CASE WHEN MC.type_txt = 'ZAB' THEN 'BII' ELSE 'STD' END) AS TB1 on CASE WHEN MC.fixed_date_dat IS NULL THEN cast('01.01.2007' as date) + MC.rel_shift_NR DAY ELSE MC.fixed_date_dat END
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.