473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 188917
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
4
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...
14
by: joshsackett | last post by:
I have a WHERE clause that could be an "=" or a "LIKE" depending upon if the passed variable is populated or not. I would like to know the best way to write the WHERE clause to make it dynamically...
2
gateshosting
by: gateshosting | last post by:
I thought this syntax was correct, but I can't figure out why it won't work. Maybe it just is not supported? CREATE PROCEDURE spTEST @includepaid bit AS SELECT FIELD FROM TABLE WHERE SOLD =...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
1
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...
2
by: pintu | last post by:
Hi Friends, I want to use "in" clause in case statement as shown below. e.g select * from employee where empid in case i_desgn when 'HR' then (1,2,3,4,5) when 'MD' then 1 else 7 end
4
by: laurenquantrell | last post by:
Is there an equivalant construction to the CASE WHEN statement that can be used in the WHERE clause? For example, this works: SELECT FirstName = CASE WHEN c.FirstName = 'Bob' THEN 'Robert'...
2
by: Napsterr | last post by:
Following is the Code I am trying Declare @Day Int Set @Day=1 Select Mobile, Contact As Name, InTimeDay1 From uvw_UserDetails
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.