
- 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
Place a specific value for NULL values in a MySQL column
Use IFNULL() to find and place a specific value for NULL values. Let us first create a table −
mysql> create table DemoTable1878 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1878 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1878 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1878 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1878 values(NULL); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1878;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Chris | | NULL | | David | | NULL | +-----------+ 4 rows in set (0.00 sec)
Here is the query to work with IFNULL() −
mysql> select ifnull(FirstName,'Robert') from DemoTable1878;
This will produce the following output −
+----------------------------+ | ifnull(FirstName,'Robert') | +----------------------------+ | Chris | | Robert | | David | | Robert | +----------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Set a specific value for the first three column values in MySQL?
- Compare for NULL value and display value 1 for these values in a new MySQL column?
- Check for NULL or NOT NULL values in a column in MySQL
- Select a specific value between two column values in MySQL?
- Set value only for NULL values in a MySQL table
- Set a custom value for NULL or empty values in MySQL
- MySQL update column to NULL for blank values
- Swap a specific column value in MySQL
- Fix a specific column value and display random values for rest of the rows in MySQL
- Get multiple count in a single MySQL query for specific column values
- Implement two conditions for a single column in MySQL for null and empty value
- Display NULL and NOT NULL records except a single specific value in MySQL
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
- Fetch a specific column value (name) in MySQL
- MySQL CASE statement to place custom values in place of NULL

Advertisements