Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL value for int in Update statement

Tags:

sql

sql-update

Is it possible to set NULL value for int column in update statement?

How can I write the update statement in this case?

like image 370
Maanu Avatar asked May 19 '11 05:05

Maanu


People also ask

How do you handle NULL in UPDATE query?

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.

Can int value be NULL in SQL?

You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints.

What is on UPDATE set NULL?

SET NULL. It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is set to NULL when the parent data is deleted or updated.

Can we UPDATE a value to NULL 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.


2 Answers

Assuming the column is set to support NULL as a value:

UPDATE YOUR_TABLE    SET column = NULL 

Be aware of the database NULL handling - by default in SQL Server, NULL is an INT. So if the column is a different data type you need to CAST/CONVERT NULL to the proper data type:

UPDATE YOUR_TABLE    SET column = CAST(NULL AS DATETIME) 

...assuming column is a DATETIME data type in the example above.

like image 85
OMG Ponies Avatar answered Sep 20 '22 10:09

OMG Ponies


By using NULL without any quotes.

UPDATE `tablename` SET `fieldName` = NULL; 
like image 25
Gaurav Avatar answered Sep 18 '22 10:09

Gaurav