I have a field that may be null which is valid, and I am finding
something I didnt expect when working with nulls.
SELECT NULL/4.0 will return NULL (which I expect), however, when I test
it with a case it does not:
SELECT NULL/4.0 AS 'TEST1', TEST2 = CASE NULL/4.0 WHEN NULL THEN
'HURAY' ELSE 'OH DARN' END
I can work around by testing for NULL first else CASE ..., but I'd like
to understand why the CASE does not test for null.
TIA
rob
SQL 2005 Enterprise x64, SP1 3 3850
Use a searched CASE expression with IS NULL. The way you're doing it is
effectively doint the comparison NULL/4.0 = NULL, which evaluates to
UNKNOWN, which means your CASE will drop through to the next WHEN (or the
ELSE in this case). NULL comparisons need to use IS NULL or IS NOT NULL.
SELECT NULL/4.0 AS 'TEST1', TEST2 =
CASE
WHEN NULL/4.0 IS NULL THEN 'HURAY'
ELSE 'OH DARN'
END
"rcamarda" <ro*****@hotmai l.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
>I have a field that may be null which is valid, and I am finding
something I didnt expect when working with nulls.
SELECT NULL/4.0 will return NULL (which I expect), however, when I test
it with a case it does not:
SELECT NULL/4.0 AS 'TEST1', TEST2 = CASE NULL/4.0 WHEN NULL THEN
'HURAY' ELSE 'OH DARN' END
I can work around by testing for NULL first else CASE ..., but I'd like
to understand why the CASE does not test for null.
TIA
rob
SQL 2005 Enterprise x64, SP1
DOH, forgot about IS
Thanks Mike
Mike C# wrote:
Use a searched CASE expression with IS NULL. The way you're doing it is
effectively doint the comparison NULL/4.0 = NULL, which evaluates to
UNKNOWN, which means your CASE will drop through to the next WHEN (or the
ELSE in this case). NULL comparisons need to use IS NULL or IS NOT NULL.
SELECT NULL/4.0 AS 'TEST1', TEST2 =
CASE
WHEN NULL/4.0 IS NULL THEN 'HURAY'
ELSE 'OH DARN'
END
"rcamarda" <ro*****@hotmai l.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
I have a field that may be null which is valid, and I am finding
something I didnt expect when working with nulls.
SELECT NULL/4.0 will return NULL (which I expect), however, when I test
it with a case it does not:
SELECT NULL/4.0 AS 'TEST1', TEST2 = CASE NULL/4.0 WHEN NULL THEN
'HURAY' ELSE 'OH DARN' END
I can work around by testing for NULL first else CASE ..., but I'd like
to understand why the CASE does not test for null.
TIA
rob
SQL 2005 Enterprise x64, SP1
The CASE expression is an *expression* and not a control statement;
that is, it returns a value of one datatype. SQL-92 stole the idea and
the syntax from the ADA programming language. Here is the BNF for a
<case specification>:
<case specification:: = <simple case| <searched case>
<simple case::=
CASE <case operand>
<simple when clause>...
[<else clause>]
END
<searched case::=
CASE
<searched when clause>...
[<else clause>]
END
<simple when clause::= WHEN <when operandTHEN <result>
<searched when clause::= WHEN <search conditionTHEN <result>
<else clause::= ELSE <result>
<case operand::= <value expression>
<when operand::= <value expression>
<result::= <result expression| NULL
<result expression::= <value expression>
The searched CASE expression is probably the most used version of the
expression. The WHEN ... THEN ... clauses are executed in left to
right order. The first WHEN clause that tests TRUE returns the value
given in its THEN clause. And, yes, you can nest CASE expressions
inside each other. If no explicit ELSE clause is given for the CASE
expression, then the database will insert a default ELSE NULL clause.
If you want to return a NULL in a THEN clause, then you must use a CAST
(NULL AS <datatype>) expression. I recommend always giving the ELSE
clause, so that you can change it later when you find something
explicit to return.
The <simple case expressionis defined as a searched CASE expression
in which all the WHEN clauses are made into equality comparisons
against the <case operand>. For example
CASE iso_sex_code
WHEN 0 THEN 'Unknown'
WHEN 1 THEN 'Male'
WHEN 2 THEN 'Female'
WHEN 9 THEN 'N/A'
ELSE NULL END
could also be written as:
CASE
WHEN iso_sex_code = 0 THEN 'Unknown'
WHEN iso_sex_code = 1 THEN 'Male'
WHEN iso_sex_code = 2 THEN 'Female'
WHEN iso_sex_code = 9 THEN 'N/A'
ELSE NULL END
There is a gimmick in this definition, however. The expression
CASE foo
WHEN 1 THEN 'bar'
WHEN NULL THEN 'no bar'
END
becomes
CASE WHEN foo = 1 THEN 'bar'
WHEN foo = NULL THEN 'no_bar' -- error!
ELSE NULL END
The second WHEN clause is always UNKNOWN.
The SQL-92 Standard defines other functions in terms of the CASE
expression, which makes the language a bit more compact and easier to
implement. For example, the COALESCE () function can be defined for
one or two expressions by
1) COALESCE (<value exp #1>) is equivalent to (<value exp #1>)
2) COALESCE (<value exp #1>, <value exp #2>) is equivalent to
CASE WHEN <value exp #1IS NOT NULL
THEN <value exp #1>
ELSE <value exp #2END
then we can recursively define it for (n) expressions, where (n >= 3),
in the list by
COALESCE (<value exp #1>, <value exp #2>, . . ., n), as equivalent to:
CASE WHEN <value exp #1IS NOT NULL
THEN <value exp #1>
ELSE COALESCE (<value exp #2>, . . ., n)
END
Likewise, NULLIF (<value exp #1>, <value exp #2>) is equivalent to:
CASE WHEN <value exp #1= <value exp #2>
THEN NULL
ELSE <value exp #1END
It is important to be sure that you have a THEN or ELSE clause with a
datatype that the compiler can find to determine the highest datatype
for the expression.
A trick in the WHERE clause is use it for a complex predicate with
material implications.
WHERE CASE
WHEN <search condition #1>
THEN 1
WHEN <search condition #2>
THEN 1
...
ELSE 0 END = 1
Gert-Jan Strik posted some exampels of how ISNULL() and COALESCE() on
2004 Aug 19
CREATE TABLE #t(a CHAR(1));
INSERT INTO #t VALUES (NULL);
SELECT ISNULL(a,'abc') FROM #t;
SELECT COALESCE(a, 'abc') FROM #t;
DROP TABLE #t;
He always use COALESCE, with the exception of the following type of
situation, because of its performance consequences:
SELECT ...,
ISNULL((SELECT COUNT(*) -- or other aggregate
FROM B
WHERE B.key = A.key), 0)
FROM A;
Likewise, Alejandro Mesa cam up with this example:
SELECT 13 / COALESCE(CAST(N ULL AS INTEGER), 2.00); -- promote to
highest type (decimal)
SELECT 13 / ISNULL(CAST(NUL L AS INTEGER), 2.00); -- promote to first
type (integer) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dan Perlman |
last post by:
From: "Dan Perlman" <dan@dpci.NOSPAM.us>
Subject: ODBC creating nulls?
Date: Friday, July 09, 2004 10:43 AM
Hi,
Below is my VB6 code that writes data from an Access 2000 table to a PG
table.
The " & "" " on the right of each line should prevent nulls from being
|
by: Alex |
last post by:
Hi folks,
I'm doing calculations based on data in a table, but the data has some
zeros in the field I'm dividing by. I'm trying to write a script to
replace any field with 0 or null with 1, but it's not working. HEre's
what I've got:
Update A Set A.deptcode = A.deptcode,
A.type = A.Type,
A.Volume = (case A.Volume
|
by: Steve Walker |
last post by:
Hi all.
I've been tasked with "speeding up" a mid-sized production system. It
is riddled with nulls... "IsNull" all over the procs, etc.
Is it worth it to get rid of the nulls and not allow them in the
columns anymore?
If so, how to go about removing the nulls with a script?
|
by: aaj |
last post by:
Hi
I am probably going to regret asking this because I'm sure you are going to
tell me my design is bad 8-) ah well we all have to learn....
anyway
I often use Nulls as a marker to see if certain tasks have been completed.
A typical example would be a column say invoice_value
|
by: mike |
last post by:
I'm doing what I thought was a simple GROUP BY summary of fairly simple
data and the my numbers aren't working out
Some results are showing up <NULL> when I know the data is in the
database
I'm no SQL expert, but if I'm summing (SUM) multiple fields and adding
them together in my SELECT how does SUM handle Null? In some situations a
single column in a single row is Null but and it's part of a larger GROUP BY
and SUM and from looking...
| |
by: Aaron Smith |
last post by:
I have a form, oddly enough the same one with the expression column that
is giving me a headache, and on this form I have a dataset that has a
couple of tables in it that are related. If I do a AddNew on the Binding
Context for the parent, it doesn't seem to work. If I click the new
(just does an addnew, and thats it) the fields do not clear out, and if
I click new again, it just says that the first field that doesn't allow
nulls, doesn't...
|
by: Jimmy |
last post by:
If the user dbl clicks the field "vendor" I want it to open frmContacts to a
blank record if there is nothing in the current field and if there is, open
frm Contacts to that record. I have the following code...
If Me.Vendor = Null Then
DoCmd.OpenForm "frmContacts", , , , acFormAdd
Else
DoCmd.OpenForm "frmContacts", , , "ClientID = " & Me!Vendor
End If
|
by: Cliff72 |
last post by:
I need to fill in the nulls in the batch field the value from the
record immediately
preceding the null one ie replace the nulls with the preceding value
until I hit a record with a value in it--then hold the next value
through the next set of nulls, and so on.
See example below:
I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10.
Then copy down batch "7138" all the way to ID 20 and so on....
|
by: Brett Barry: Go Get Geek! |
last post by:
Hello,
I'm having this problem and tried the answer in this post:
http://groups.google.com/group/comp.databases.ms-access/browse_thread/thread/fba938d86b839345/a808c90cac4328ff?hl=en&lnk=gst&q=convert+null+values+to+zero#a808c90cac4328ff
However, when I build the expression I get an error message that I
have the wrong number of arguments. Can someone tell me what is wrong
with this code?:
TotalOpenOrders: IIf(IsNull(Sum(!
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
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...
|
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...
|
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();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |