468,310 Members | 1,380 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,310 developers. It's quick & easy.

CASE Statement in Where Clause?

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
Jul 20 '05 #1
2 188510
"Largo SQL Tools" <support@_REMOVE_largosqltools.com> wrote in message
news:v5********************@buckeye-express.com...
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


CASE is an expression, not a statement, and, as such, returns a value.
It can indeed be used in a WHERE clause. For example,

SELECT *
FROM T
WHERE col1 = CASE WHEN col2 < col3
THEN col2
ELSE col3
END

Regards,
jag
Jul 20 '05 #2
Sure you can. Unlike procedural languages, CASE in SQL is an expression. You
can use almost any kind of expressions in WHERE clause (aggregate functions
are exceptions).

This is an example of using CASE in WHERE clause:

select *
from YourTable
where SomeColumn = case
when Condition1 then Value1
when Condition2 then Value2
else Value3
end
Shervin

"Largo SQL Tools" <support@_REMOVE_largosqltools.com> wrote in message
news:v5********************@buckeye-express.com...
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

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by mirth | last post: by
14 posts views Thread by joshsackett | last post: by
1 post views Thread by priyanka2203 | last post: by
2 posts views Thread by pintu | last post: by
4 posts views Thread by laurenquantrell | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.