

- 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
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 Questions & Answers
- MySQL query to order by NULL values
- MySQL query to convert empty values to NULL?
- How to select multiple max values which would be duplicate values as well in MYSQL?
- MySQL query to set values for NULL occurrence
- How to handle Null values while working with JDBC?
- MySQL query to display only the empty and NULL values together?
- How to skip blank and null values in MySQL?
- How to count null values in MySQL?
- MySQL query to compare and display only the rows with NULL values
- Add values of two columns considering NULL values as zero in MySQL
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query to count the dates and fetch repeated dates as well
- How to use a single MySQL query to count column values ignoring null?
- MySQL query to replace only the NULL values from the table?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
Advertisements