473,654 Members | 3,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Case Statement in Where Clause

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 tell what i'm after

the first part should evaluate a boolean expression, then if true,
search using one field, otherwise
search using a different field

WHERE

Case WHEN @myCompany = 933
THEN tblClient.compa nycode = 933 --problem line
ELSE
tblCase.clientc ode = @myClient --problem line
END

And tblCase.status = 'Active'

thank you!!

Oct 12 '06 #1
9 10940
In SQL Server, CASE doesn't conditionally execute statements, it returns an
expression. You might try something like:

WHERE
tblClient.compa nycode =
CASE
WHEN @myCompany = 933
THEN 933 ELSE tblClient.compa nycode END
AND
tblCase.clientc ode =
CASE
WHEN @myCompany < 933
THEN @myClient ELSE tblCase.clientc ode END
AND tblCase.status = 'Active'

--
Hope this helps.

Dan Guzman
SQL Server MVP
<pa********@gma il.comwrote in message
news:11******** *************@i 3g2000cwc.googl egroups.com...
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 tell what i'm after

the first part should evaluate a boolean expression, then if true,
search using one field, otherwise
search using a different field

WHERE

Case WHEN @myCompany = 933
THEN tblClient.compa nycode = 933 --problem line
ELSE
tblCase.clientc ode = @myClient --problem line
END

And tblCase.status = 'Active'

thank you!!
Oct 13 '06 #2
Dan Guzman wrote:
In SQL Server, CASE doesn't conditionally execute statements, it returns
an expression. You might try something like:

WHERE
tblClient.compa nycode =
CASE
WHEN @myCompany = 933
THEN 933 ELSE tblClient.compa nycode END
AND
tblCase.clientc ode =
CASE
WHEN @myCompany < 933
THEN @myClient ELSE tblCase.clientc ode END
AND tblCase.status = 'Active'
Another alternative:

WHERE (NOT(@myCompany = 933) OR tblClient.compa nycode = 933)
AND (NOT(@myCompany <933) OR tblCase.clientc ode = @myClient)
AND tblCase.status = 'Active'
Oct 13 '06 #3
There is no CASE statement in SQL; there is a CASE expression.
Expressions return values. There is no BOOLEAN data type in SQL.
Fields are not anything like columns. Your entire mental model of SQL
is wrong and you are trying to make it work like a procedural language
that you already know.

Also, we prefer to follow ISO-11179 rules for data elements names, so
quit putting that silly, redundant "tbl-" on table names (unless
furniture is actually involved) and start using collective or plural
names (unless the table is not a set or class of entities, but a single
item).

Here is a guess at what you wanted

WHERE ( COALESCE (@my_company , Clients.company _code) = 933
OR Cases.clientcod e = @my_client)
AND Case.foobar_sta tus = 'Active' -- status is too vague

Oct 13 '06 #4
I don't understand the necessity of the diatribe, but thanks for the
suggestion.

Oct 13 '06 #5
This first 2 suggestions worked perfectly. The third offering returned
unwanted records.

Thanks for your help. I really appreciate it.

Oct 13 '06 #6
>I don't understand the necessity of the diatribe,<<

EDUCATION ! Your approach is wrong and you are going to kill people or
ruin companies until you get a better mindset. Get over your ego and
become professional. It takes six years to become a Union Journeryman
Carpenter in New York State, but newbies expect to do databases
immeidtely after they get MS certificates. Would you like medicine to
work this way?

You are welcome!

Oct 14 '06 #7
OK, at least that one was funny.

No ego here, I have to admit you peeked my interest. I've been doing
database work for 10 years now. I've managed to avoid killing people
and ruining companies at least up till the time of this posting.

I do always like to learn new things or better ways to do things.

So, having said that, I'm really curious as to who "we" are that you
reference in your original post. And where can I go to get information
on things like "ISO-11179 rules for data elements names" or another
site to fix my mindset?

thanks,
Paul

btw, Boolean is right there in online books

Searched CASE function:

CASE
WHEN Boolean_express ion THEN result_expressi on
[ ...n ]
[
ELSE else_result_exp ression
]
END

Oct 17 '06 #8
pa********@gmai l.com wrote:
So, having said that, I'm really curious as to who "we" are that you
reference in your original post. And where can I go to get information
on things like "ISO-11179 rules for data elements names" or another
site to fix my mindset?
Googling "ISO-11179" turns up http://metadata-standards.org/11179/
and in particular http://metadata-standards.org/11179/#11179-5

"Part 5: Naming and Identification Principles, provides guidance for the
identification of administered items. Identification is a broad term
for designating, or identifying, a particular data item. Identification
can be accomplished in various ways, depending upon the use of the
identifier. Identification includes the assignment of numerical
identifiers that have no inherent meanings to humans; icons (graphic
symbols to which meaning has been assigned); and names with embedded
meaning, usually for human understanding, that are associated with the
data item's definition and value domain."
btw, Boolean is right there in online books

Searched CASE function:

CASE
WHEN Boolean_express ion THEN result_expressi on
[ ...n ]
[
ELSE else_result_exp ression
]
END
That's an expression, not a store-able data type. Again, Googling
(SQL "Boolean type") turns up http://troels.arvin.dk/db/rdbms/
and in particular http://troels.arvin.dk/db/rdbms/#data_types-boolean

Standard:

"The BOOLEAN type is optional (has feature ID T031), which is a bit
surprising for such a basic type. However, it seems that endless
discussions of how NULL is to be interpreted for a boolean value is
holding BOOLEAN from becoming a core type.

The standard says that a BOOLEAN may be one of the following literals:

* TRUE
* FALSE
* UNKNOWN or NULL (unless prohibited by a NOT NULL constraint)

The DBMS may interpret NULL as equivalent to UNKNOWN. It is unclear
from the specification if the DBMS must support UNKNOWN, NULL or both
as boolean literals. In this author's opinion, you should forget about
the UNKNOWN literal in order to simplify the situation and let the
normal SQL three-way logic apply.

It's defined that TRUE FALSE (true larger than false)."

MSSQL:

"Doesn't support the BOOLEAN type.

Possible alternative type: the BIT type which may have 0 or 1 (or NULL)
as value. If you insert an integer value other than these into a field
of type BIT, then the inserted value will silently be converted to 1.

Rudy Limeback has some notes
[http://searchoracle.techtarget.com/a...tionNResponse/
0,289625,sid41_ cid556536_tax30 1455,00.html]
about oddities with the MSSQL BIT type.

Documentation [http://msdn2.microsoft .com/en-us/library/ms177603.aspx]"
Oct 18 '06 #9
>where can I go to get information on things like "ISO-11179 rules for data elements names" or another site to fix my mindset? <<

My books and Chris Date's stuff deal with the "mindset problem" -- I
like my books better, but his "What, not How" is a good intro to
thinking in declarations rather then procedures. You can Google
ISO-11179 specs, but my SQL PROGRAMMING STYLE has some practical tips
on using the principles and is eaier to read than "Standard-speak".
>btw, Boolean is right there in online books <<
And they are once again wrong. Standard SQL uses <search conditionin
the BNF; we were careful not to say <predicateor <Boolean Expression>
because of the 3-valued logic and the lack of inference rules in SQL.

Oct 18 '06 #10

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

Similar topics

2
188929
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 http://www.largosqltools.com
1
7131
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 Glcodes. The problem is I keep getting multiple rows returned errors. "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been...
3
21994
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 TELL ME IF IT IS POSSIBLE TO USE IT IN WHERE CLAUSE AND SOME ALTERNATIVE. QUERY: SELECT
4
28818
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 select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on this with relation to the following example (is there a way to acheive this without re-stating the...
9
2354
by: Michael | last post by:
Hi all, I would like to get people's opinion about executing SQL statements in C# (or any other .NET language really). I used to create my SQL statement by building a string and replacing single quote with two single quotes. Sometimes, I used SqlParameter. Maybe, I'm a bit lazy when I build the SQL string. Should I always use SqlParameters? What are the advantages/disadvantages between building SQL string and using SqlParameter? Does...
8
2474
by: Jeff Gilbert | last post by:
Hello all. I'd appreciate some help with this one: First the DDL: CREATE TABLE ( NOT NULL , NULL , NOT NULL , (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL CONSTRAINT DEFAULT
7
6213
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 statement. pOrder as input value where pOrder can be 1 or 0
2
544
by: iulian.ilea | last post by:
What is wrong int this SQL statement? select top 10 DOCInt.*, DOCDet.* , Cate.*, Arti.*, .* from DOCInt INNER JOIN DOCDet ON DOCInt.CodDoc=DOCDet.CodDoc LEFT JOIN Cate ON DOCDet.IDCategory=Cate. LEFT JOIN Arti ON DOCDet.IDArti=Arti. INNER JOIN ON DOCInt.IDAnag=. GROUP BY DOCInt.IDDoc
1
5001
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 (simple-case-statement-when-clause), in this - the value of the expression prior to the first WHEN keyword is tested for equality with the value of each expression that follows the WHEN keyword. Right? Then if the search condition is true, the THEN...
5
15496
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 MC.type_txt = 'ZAB' THEN 'BII' ELSE 'STD' END) AS TB1 on CASE WHEN MC.fixed_date_dat IS NULL THEN cast('01.01.2007' as date) + MC.rel_shift_NR DAY ELSE MC.fixed_date_dat END
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.