473,242 Members | 1,793 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,242 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 188905
"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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.