473,396 Members | 2,076 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,396 software developers and data experts.

CASE WHEN Equivalent in WHERE Clause?

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'
ELSE
c.FirstName
END,
c.LastName
FROM
Contacts c
WHERE
(c.FirstName = 'Bob')
OR
(c.FirstName = 'Robert')

But is there a way to build somehting resembling this (without using
Exec-SQL)

@FirstName nvarchar(35)

SELECT
c.FirstName
c.LastName
FROM
Contacts c
WHERE
CASE WHEN @FirstName = 'Bob' THEN
c.BlueHair = 1
ELSE CASE WHEN @FirstName = 'Frank' THEN
c.PastaEater = 1
ELSE
c.HatSize 5
END END

Mar 28 '08 #1
4 3874
You have to change your CASE so it properly returns expression:

SELECT c.FirstName,
c.LastName
FROM Contacts AS c
WHERE
CASE WHEN @FirstName = 'Bob' THEN
CASE WHEN c.BlueHair = 1 THEN 'Y' END
ELSE
CASE WHEN @FirstName = 'Frank' THEN
CASE WHEN c.PastaEater = 1 THEN 'Y' END
ELSE
CASE WHEN c.HatSize 5 THEN 'Y' END
END
END = 'Y'

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Mar 28 '08 #2
>Is there an equivalent construction to the CASE WHEN statement that can be used in the WHERE clause? <<

1) There is no CASE statement in SQL; but we do have a CASE
expression. You still have a procedural mindset and have not started
thinking in sets.

2) I am guessing that you want to write a **predicate** something like
this:

DECLARE @FirstName NVARCHAR(35);

SELECT first_name, last_name
FROM Contacts
WHERE
CASE WHEN @first_name = 'Bob' AND bluehair = 1
THEN 'T'
WHEN @first_name = 'Frank' AND pastaeater = 1
THEN 'T'
WHEN hat_size 5.0
THEN 'T' ELSE 'F' END = 'T';

Mar 28 '08 #3
On Fri, 28 Mar 2008 11:40:05 -0700 (PDT), --CELKO-- wrote:
>>Is there an equivalent construction to the CASE WHEN statement that can be used in the WHERE clause? <<

1) There is no CASE statement in SQL; but we do have a CASE
expression. You still have a procedural mindset and have not started
thinking in sets.

2) I am guessing that you want to write a **predicate** something like
this:

DECLARE @FirstName NVARCHAR(35);

SELECT first_name, last_name
FROM Contacts
WHERE
CASE WHEN @first_name = 'Bob' AND bluehair = 1
THEN 'T'
WHEN @first_name = 'Frank' AND pastaeater = 1
THEN 'T'
WHEN hat_size 5.0
THEN 'T' ELSE 'F' END = 'T';
Hi Joe,

This is not equivalent to what Lauren Quantrell wanted. The version
posted by Plamen Ratchev is better.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Mar 30 '08 #4
la*************@gmail.com wrote:
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'
ELSE
c.FirstName
END,
c.LastName
FROM
Contacts c
WHERE
(c.FirstName = 'Bob')
OR
(c.FirstName = 'Robert')

But is there a way to build somehting resembling this (without using
Exec-SQL)

@FirstName nvarchar(35)

SELECT
c.FirstName
c.LastName
FROM
Contacts c
WHERE
CASE WHEN @FirstName = 'Bob' THEN
c.BlueHair = 1
ELSE CASE WHEN @FirstName = 'Frank' THEN
c.PastaEater = 1
ELSE
c.HatSize 5
END END
If you have this few @FirstName values with special rules, then you
could also rewrite it like this:

WHERE (@FirstName = 'Bob' AND c.BlueHair = 1)
OR (@FirstName = 'Frank' AND c.PastaEater = 1)
OR (NOT (@FirstName IN ('Bob', 'Frank')) AND c.HatSize 5)

Note that the third line is not written as
OR (@FirstName NOT IN ('Bob', 'Frank') AND c.HatSize 5)
because @FirstName null would fail to satisfy it.
Mar 31 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
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...
3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
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...
4
by: Seenu | last post by:
I'm experiencing some puzzling behaviour with some of my UDFs when declaring them as ATOMIC.. Basically I'm invoking another UDF (which uses some Java code) in one branch of a CASE statment, and...
7
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...
9
by: paulmac106 | last post by:
If you could help me with my syntax i would really appreciate it. I'm trying to change the where clause on the fly, but it's giving me a syntax error. Hopefully from what I have below you can...
5
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...
6
by: Derik | last post by:
Okay, I THINK this is a PHP question... I've been mucking with PHP for awhile now, but just recently I've been poking at some ajax stuff, and I ran into something confusing; my Queries were...
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
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
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.