
- 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
Set a custom value for NULL or empty values in MySQL
Let us first create a table −
mysql> create table DemoTable ( Value varchar(100) ); Query OK, 0 rows affected (0.75 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('100'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('200'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(null); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 100 | | | | 200 | | NULL | +-------+ 4 rows in set (0.00 sec)
Following is the query to set a custom value for NULL or empty values −
mysql> select if(Value IS NULL or Value='','Garbage Value',Value) AS Result from DemoTable;
This will produce the following output −
+---------------+ | Result | +---------------+ | 100 | | Garbage Value | | 200 | | Garbage Value | +---------------+ 4 rows in set (0.00 sec)
- Related Articles
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- Set value only for NULL values in a MySQL table
- Set multiple values for custom columns in MySQL?
- Set custom messages for enum values in MySQL
- Check for NULL or empty variable in a MySQL stored procedure
- Check for NULL or NOT NULL values in a column in MySQL
- How MySQL handles the empty and null values for enumerations?
- Set 1 for NOT NULL value in MySQL
- Implement two conditions for a single column in MySQL for null and empty value
- MySQL query to set values for NULL occurrence
- Place a specific value for NULL values in a MySQL column
- Checking for Null or Empty in Java.
- Using CASE statement in MySQL to display custom name for empty value
- Check whether a field is empty or null in MySQL?
- How to set default value for empty row in MySQL?

Advertisements