
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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)
- Related Questions & Answers
- How to skip blank and null values in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- MySQL queries to update date records with NULL values
- Treat a MySQL column field as NULL if it is blank?
- Place a specific value for NULL values in a MySQL column
- MySQL query to set values for NULL occurrence
- Display 1 for NULL values in MySQL
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- Update one column data to another column in MySQL if the second column is NOT NULL?
- Updating blank cells to NULL will cause all cells to be NULL in MySQL?
- How do I update NULL values in a field in MySQL?
- How to check for null, undefined, or blank variables in JavaScript?
- Update all the fields in a table with null or non-null values with MySQL
- Compare for NULL value and display value 1 for these values in a new MySQL column?
- How to update a MySQL table by swapping two column values?
Advertisements