
- 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 null value with empty string in several columns while fetching data
For this, you can use IFNULL() or COALESCE(). Let us first create a table −
mysql> create table DemoTable1849 ( ClientFirstName varchar(20), ClientLastName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1849 values('John',NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1849 values(NULL,'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1849 values(NULL,NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1849 values('Chris','Brown'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1849;
This will produce the following output −
+-----------------+----------------+ | ClientFirstName | ClientLastName | +-----------------+----------------+ | John | NULL | | NULL | Miller | | NULL | NULL | | Chris | Brown | +-----------------+----------------+ 4 rows in set (0.00 sec)
Here is the query to replace null value with empty string in several columns while fetching data −
mysql> select ifnull(ClientFirstName,'') as ClientFirstName,ifnull(ClientLastName,'') as ClientLastName from DemoTable1849;
This will produce the following output −
+-----------------+----------------+ | ClientFirstName | ClientLastName | +-----------------+----------------+ | John | | | | Miller | | | | | Chris | Brown | +-----------------+----------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL select query to fetch data with null value?
- MySQL query to convert empty values to NULL?
- How to update empty string to NULL in MySQL?
- Empty string in not-null column in MySQL?
- Replace 0 with null in MySQL?
- How to replace 'Empty set' in a MySQL query?
- MySQL query to display custom text for empty columns
- MySQL query to replace a column value
- MySQL query to display only the empty and NULL values together?
- MySQL query to replace only the NULL values from the table?
- MongoDB query to replace value with aggregation?
- Replace the empty values from a MySQL table with a specific value
- How to find records with a null value in a set of columns with MySQL
- How to retrieve the corresponding value for NULL with a MySQL query?
- Find the count of EMPTY or NULL columns in a MySQL table?

Advertisements