Update columns with Null values
Asked Answered
H

5

38

I tried updating a table as follows:

update userloginstats set logouttime = sysdate where logouttime = null;

It didn't update the columns with null values. What went wrong?

Hog answered 13/10, 2010 at 12:42 Comment(1)
<resisting temptation to add yet another answer that says exactly the same thing...>Kedge
R
80

Change it to

...where logouttime is null;
                    ^^^^^^^

NULL is a special value and we cannot use the usual = operator with it.

From the Oracle documentation for NULL:

To test for nulls, use only the comparison conditions IS NULL and IS NOT NULL. If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN because null represents a lack of data, a null cannot be equal or unequal to any value or to another null

Rapper answered 13/10, 2010 at 12:44 Comment(0)
M
11

You cannot compare NULLs with =.

Use this:

update userloginstats set logouttime= sysdate where logouttime is null;
Major answered 13/10, 2010 at 12:44 Comment(0)
H
7

logouttime is null, not = null. null is never equal to anything, not even itself. Thus, the operator is.

Haerle answered 13/10, 2010 at 12:44 Comment(0)
S
5

You need to use is null not = null

update userloginstats set logouttime= sysdate where logouttime is null;
Spoliate answered 13/10, 2010 at 12:44 Comment(0)
C
3

For nulls you must use "IS NULL" or "IS NOT NULL" rather than the = operator. This is because null is technically neither true or false, rather it's the absence of a value either way.

Most programming languages typically associate null with false for convenience (and thereby enabling the use of the = operator), but SQL takes a more purist approach, rightly or wrongly.

Car answered 13/10, 2010 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.