
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to replace only the NULL values from the table?
For this, you can use the property IS NULL for null values in MySQL. Let us first create a table −
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Name | +--------+ | Robert | | NULL | | David | | NULL | | Robert | +--------+ 5 rows in set (0.00 sec)
Following is the query to replace null values from the table −
mysql> update DemoTable set Name=IF(Name IS NULL,'Please Enter a Name',Name); Query OK, 2 rows affected (0.09 sec) Rows matched: 5 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | Name | +---------------------+ | Robert | | Please Enter a Name | | David | | Please Enter a Name | | Robert | +---------------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL query to display only the empty and NULL values together?
- MySQL query to replace only the backslashes from folder path?
- MySQL query to compare and display only the rows with NULL values
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- MySQL query to exclude some of the values from the table
- Set value only for NULL values in a MySQL table
- MySQL query to order by NULL values
- MySQL query to skip the duplicate and select only one from the duplicated values
- Replace the empty values from a MySQL table with a specific value
- MySQL query to convert empty values to NULL?
- Remove only the percentage sign from values in a MySQL table?
- MySQL query to set values for NULL occurrence
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- MySQL replace values in a table?

Advertisements