- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL CASE statement to place custom values in place of NULL
Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(20) ); Query OK, 0 rows affected (1.15 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | John | | NULL | | Adam | | NULL | +-----------+ 4 rows in set (0.00 sec)
Let us now use CASE statement −
mysql> select case when FirstName is NULL then 'UNKNOWN NAME' else FirstName end AS FirstName from DemoTable;
This will produce the following output −
+--------------+ | FirstName | +--------------+ | John | | UNKNOWN NAME | | Adam | | UNKNOWN NAME | +--------------+ 4 rows in set (0.00 sec)
We can also get the same results using IFNULL() −
mysql> select IFNULL(FirstName,'UNKNOWN NAME') AS FirstName from DemoTable;
This will produce the following output −
+--------------+ | FirstName | +--------------+ | John | | UNKNOWN NAME | | Adam | | UNKNOWN NAME | +--------------+ 4 rows in set (0.00 sec)
- Related Articles
- Place a specific value for NULL values in a MySQL column
- In which conditions, MySQL CASE statement return NULL?
- MySQL query to update only a single field in place of NULL
- Using CASE statement in MySQL to display custom name for empty value
- Write MySQL case statement to set custom messages for student’s result
- Set a custom value for NULL or empty values in MySQL
- What MySQL MAKE_SET() function returns if there are all NULL at the place of strings?
- Display custom text in a new column on the basis of null values in MySQL?
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- Conditional NOT NULL case MySQL?
- How to match one character in MySQL in place of %?
- How to use NULL in MySQL SELECT statement?
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
- Inserting empty string in place of repeating values in JavaScript
- MySQL case statement inside a select statement?

Advertisements