Solved

operand case statement in a where clause, advanced search

Posted on 2007-03-26
3
532 Views
Last Modified: 2010-03-20
I have an advanced search form where I want a user to be able to choose their operand (>, <, >=, <=, =) for the rating of a tutorial (1,2,3,4,5). The operand and the rating int get passed into the stored proc. What I want to do is use a case statement in the where clause that gives them the operand they chose without having to write multiple sql statement. As a simple example (tried in query analyzer) I'm trying to do the following but am getting an error saying "Incorrect syntax near '>'. Can someone explain what I'm doing wrong. Thanks.
------------------------
DECLARE @what varchar(5)
SET @what = '>'

select @what

select tutorial_id, tutorial_title, tutorial_description
from uw_toots
where
avgrating = (CASE when @what = '>' then > else = END) @avgrating
order by tutorial_id

0
Comment
Question by:jrmcintosh
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 18796638
select tutorial_id, tutorial_title, tutorial_description
from uw_toots
where (avgrating => @avgrating and @what = '>' ) or (avgrating = @avgrating and @what = '=' )
order by tutorial_id
0
 
LVL 9

Expert Comment

by:dbeneit
ID: 18796641
You can't use a operator in case.
try
DECLARE @what varchar(5)
SET @what = '>'

select @what

select tutorial_id, tutorial_title, tutorial_description
from uw_toots
where
(CASE when @what = '>' then avgrating = > @avgrating else avgrating = @avgrating END)
order by tutorial_id
0
 
LVL 10

Accepted Solution

by:
lahousden earned 250 total points
ID: 18797397
Like dbeneit says, you can't use CASE for this - this should work, however:

select tutorial_id, tutorial_title, tutorial_description
from uw_toots
where ('>' = @what and avgrating > @avgrating)
or  ('<' = @what and avgrating < @avgrating)
or  ('>=' = @what and avgrating >= @avgrating)
or  ('<=' = @what and avgrating <= @avgrating)
or  ('=' = @what and avgrating = @avgrating)
order by tutorial_id
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now