
- 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
Display and concatenate records ignoring NULL values in MySQL
Use CONCAT() to concatenate records whereas IFNULL() to check for NULL values.
Let us first create a table −
mysql> create table DemoTable802 ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (1.01 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable802 values('Adam','Smith'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable802 values('Carol',NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable802 values(NULL,'Taylor'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable802 values(NULL,NULL); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable802;
This will produce the following output -
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | Carol | NULL | | NULL | Taylor | | NULL | NULL | +-----------+----------+ 4 rows in set (0.00 sec)
Following is the query to concatenate records ignoring the NULL values −
mysql> select concat(IFNULL(FirstName,''),' ',IFNULL(LastName,'')) AS FULL_NAME from DemoTable802;
This will produce the following output -
+------------+ | FULL_NAME | +------------+ | Adam Smith | | Carol | | Taylor | | | +------------+ 4 rows in set (0.00 sec)
- Related Articles
- Display records ignoring NULL in MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Display all records ignoring the current date record in MySQL
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- Display NULL and NOT NULL records except a single specific value in MySQL
- Ignore null values in MySQL and display rest of the values
- Display 1 for NULL values in MySQL
- MySQL queries to update date records with NULL values
- Display first non-null values with coalesce() in MySQL?
- Find “greatest” between two columns and display with some records already null in MySql
- How to use a single MySQL query to count column values ignoring null?
- Change only dates, ignoring time records in MySQL
- MySQL query to display only the empty and NULL values together?
- Concatenate two columns when one of such column values is null in MySQL
- Use NOT IN, OR and IS NULL in the same MySQL query to display filtered records

Advertisements