
- 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
Concatenate two columns when one of such column values is null in MySQL
To avoid any issues while running a query, use IFNULL(). Let us first create a table −
mysql> create table DemoTable1793 ( StudentFirstName varchar(20), StudentLastName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1793 values('John','Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1793 values('Carol',NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1793 values(NULL,'Brown'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1793;
This will produce the following output −
+------------------+-----------------+ | StudentFirstName | StudentLastName | +------------------+-----------------+ | John | Smith | | Carol | NULL | | NULL | Brown | +------------------+-----------------+ 3 rows in set (0.00 sec)
Here is the query to concatenate two columns when one of such column values is null −
mysql> select concat(ifnull(StudentFirstName,''),ifnull(StudentLastName,'')) from DemoTable1793;
This will produce the following output −
+----------------------------------------------------------------+ | concat(ifnull(StudentFirstName,''),ifnull(StudentLastName,'')) | +----------------------------------------------------------------+ | JohnSmith | | Carol | | Brown | +----------------------------------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Concatenate two columns in MySQL?
- SELECT not null column from two columns in MySQL?
- Add values of two columns considering NULL values as zero in MySQL
- Count only null values in two different columns and display in one MySQL select statement?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Display and concatenate records ignoring NULL values in MySQL
- Concatenate two values from the same column with different conditions in MySQL
- How can we overcome the property of CONCAT() function that it returns NULL if any one of the argument is NULL, especially when we want to concatenate the values from the column and any of the columns have NULL as its value?
- Multiply values of two columns and display it a new column in MySQL?
- What is the advantage of CONCAT_WS() function over CONCAT() function when we want to concatenate the values from the column and any of the columns have NULL as its value?
- How to concatenate all values of a single column in MySQL?
- Concatenate all the columns in a single new column with MySQL
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- How to select and display a list of values in one column that are available in two different MySQL columns?
- Check for NULL or NOT NULL values in a column in MySQL

Advertisements