Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update columns with Null values

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?

like image 859
nath Avatar asked Oct 13 '10 12:10

nath


People also ask

How do you UPDATE NULL values in a column?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0. Cleaning data is important for analytics because messy data can lead to incorrect analysis.

How do you UPDATE a column with blank value in SQL?

In this article, we will look into how you can set the column value to Null in SQL. update students set Gender = NULL where Gender='F'; SELECT * FROM students ; Output: Column value can also be set to NULL without specifying the 'where' condition.

How do you UPDATE multiple columns to NULL in SQL?

Update one or multiple columns of a table to null is really easy in SQL Server. [Column-Name-1] = NULL, [Column-Name-2] = NULL, ...

How do you UPDATE a column to NULL in Oracle?

Click in the column value you want to set to (null) . Select the value and delete it. Hit the commit button (green check-mark button). It should now be null.


2 Answers

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

like image 122
codaddict Avatar answered Sep 23 '22 16:09

codaddict


You cannot compare NULLs with =.

Use this:

update userloginstats set logouttime= sysdate where logouttime is null; 
like image 26
Lucero Avatar answered Sep 25 '22 16:09

Lucero