

- 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
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 Questions & Answers
- 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
- MySQL case statement inside a select statement?
- How to use MySQL CASE statement while using UPDATE Query?
- Select Statement to retrieve the same first names with similar last name (but different case) using MySQL?
- Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java
- How to set default value for empty row in MySQL?
- MySQL query to ORDER BY `user_id` IN (1,2,3) AND `name` for custom ordering
- Why should we use MySQL CASE Statement?
- Implement MySQL CASE statement with WHEN clause
- In which conditions, MySQL CASE statement return NULL?
Advertisements