
- 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
Using CASE statement in MySQL to display custom name for empty value
For this, you can use CASE WHEN statement. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Name | +-------+ | Chris | | | | David | | | +-------+ 4 rows in set (0.00 sec)
Following is the query to display custom name for empty values −
mysql> select case trim(ifnull(Name,'')) -> when '' then 'Carol Taylor' -> else -> Name -> end as Name -> from DemoTable;
This will produce the following output −
+--------------+ | Name | +--------------+ | Chris | | Carol Taylor | | David | | Carol Taylor | +--------------+ 4 rows in set (0.03 sec)
- Related Articles
- MySQL query to display custom text for empty columns
- Write MySQL case statement to set custom messages for student’s result
- Set a custom value for NULL or empty values in MySQL
- MySQL CASE statement to place custom values in place of NULL
- Change value from 1 to Y in MySQL Select Statement using CASE?
- MySQL order by field using CASE Statement
- Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java
- How to use MySQL CASE statement while using UPDATE Query?
- How to set default value for empty row in MySQL?
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
- MySQL case statement inside a select statement?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- MySQL query to ORDER BY `user_id` IN (1,2,3) AND `name` for custom ordering
- In which conditions, MySQL CASE statement return NULL?
- Perform count with CASE WHEN statement in MySQL?

Advertisements