 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL update column to NULL for blank values
For this, you can use IF() along with UPDATE command. Let us first create a table −
mysql> create table DemoTable1601 -> ( -> FirstName varchar(20) , -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1601 values('John','Doe'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1601 values('Adam',''); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1601 values('David','Miller'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1601 values('Chris',''); Query OK, 1 row affected (0.11 sec) Display all records from the table using select statement −
mysql> select * from DemoTable1601;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | Adam | | | David | Miller | | Chris | | +-----------+----------+ 4 rows in set (0.00 sec)
Following is the query to update column to null or its value −
mysql> update DemoTable1601 set LastName=if(LastName='',NULL,LastName); Query OK, 2 rows affected (0.22 sec) Rows matched: 4 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1601;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | Adam | NULL | | David | Miller | | Chris | NULL | +-----------+----------+ 4 rows in set (0.00 sec)
Advertisements
 