
- 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
How to concat Values in MySQL Query and to handle Null values as well?
You can use CONCAT() method to concatenate values while IFNULL() method is used to handle NULL values. The syntax is as follows:
SELECT CONCAT('anyStringValue:',IFNULL(yourColumnName, 'anyStringValue’)) AS anyVariableName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table ConcatValues -> ( -> Id int NOT NULL AUTO_INCREMENT, -> FirstName varchar(20), -> MiddleName varchar(20), -> LastName varchar(20), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into ConcatValues(FirstName,MiddleName,LastName) values('John' ,NULL ,'Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into ConcatValues(FirstName,MiddleName,LastName) values('Carol' ,NULL ,'Taylor'); Query OK, 1 row affected (0.16 sec) mysql> insert into ConcatValues(FirstName,MiddleName,LastName) values('David' ,NULL ,'Miller'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from ConcatValues;
The following is the output:
+----+-----------+------------+----------+ | Id | FirstName | MiddleName | LastName | +----+-----------+------------+----------+ | 1 | Mary | Elizabeth | Smith | | 2 | John | NULL | Smith | | 3 | Carol | NULL | Taylor | | 4 | David | NULL | Miller | +----+-----------+------------+----------+ 4 rows in set (0.00 sec)
Here is the query to handle the NULL and concatenate values:
mysql> select -> concat('The middle name is:',IFNULL(MiddleName, 'Not Available')) AS MiddleName -> from ConcatValues;
The following is the output:
+----------------------------------+ | MiddleName | +----------------------------------+ | The middle name is:Elizabeth | | The middle name is:Not Available | | The middle name is:Not Available | | The middle name is:Not Available | +----------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to order by NULL values
- MySQL query to convert empty values to NULL?
- MySQL query to set values for NULL occurrence
- How to handle Null values while working with JDBC?
- How to select multiple max values which would be duplicate values as well in MYSQL?
- MySQL query to display only the empty and NULL values together?
- How to count null values in MySQL?
- How to skip blank and null values in MySQL?
- MySQL query to compare and display only the rows with NULL values
- How to use a single MySQL query to count column values ignoring null?
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query to replace only the NULL values from the table?
- Add values of two columns considering NULL values as zero in MySQL
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query

Advertisements