
- 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
How can I return 0 for NULL in MySQL?
We can return 0 for NULL in MySQL with the help of IFNULL() method. The syntax of IFNULL() is as follows.
IFNULL(YOUREXPRESSION,0);
Let us see an example. First, we will create a table.
mysql> create table NullDemoWithZero -> ( -> id varchar(200) -> ); Query OK, 0 rows affected (0.65 sec)
After creating a table, let us insert some records in the table using the INSERT command. The query is as follows −
mysql> insert into NullDemoWithZero values(); Query OK, 1 row affected (0.16 sec) mysql> insert into NullDemoWithZero values('123'); Query OK, 1 row affected (0.14 sec) mysql> insert into NullDemoWithZero values('442'); Query OK, 1 row affected (0.10 sec) mysql> insert into NullDemoWithZero values('333'); Query OK, 1 row affected (0.13 sec) mysql> insert into NullDemoWithZero values(); Query OK, 1 row affected (0.15 sec) mysql> mysql> insert into NullDemoWithZero values(); Query OK, 1 row affected (0.17 sec)
Display all records with the help of SELECT statement. The query is as follows.
mysql> select *from NullDemoWithZero;
The following is the output.
+------+ | id | +------+ | NULL | | 123 | | 442 | | 333 | | NULL | | NULL | +------+ 6 rows in set (0.00 sec)
Look at the sample output above, there are many NULL values. Now, I am going to write a syntax that returns 0, if there is NULL value in the table. The following is the syntax.
select IFNULL(yourColumnName, 0) from yourTableName;
Apply the above syntax to get 0 if any row has NULL value. The query is as follows.
mysql> select IFNULL(id, 0) from NullDemoWithZero;
The following is the output that returns 0 for every NULL.
+---------------+ | IFNULL(id, 0) | +---------------+ | 0 | | 123 | | 442 | | 333 | | 0 | | 0 | +---------------+ 6 rows in set (0.00 sec)
- Related Articles
- How can I set 0 if a query returns a null value in MySQL?
- Return null for date_format when input is null in MySQL?
- Return 0 in a new column when record is NULL in MySQL?
- How can we check for NULL in a MySQL query?
- Typecasting NULL to 0 in MySQL
- Replace 0 with null in MySQL?
- Convert MySQL null to 0?
- What will MySQL CHAR_LENGTH() function return if I provide NULL to it?
- In which conditions, MySQL CASE statement return NULL?
- Sum if all rows are not null else return null in MySQL?
- How can I customize the output of MySQL SUM() function to 0 instead of NULL when there are no matching rows?
- How can we return null from a generic method in C#?
- How do I insert a NULL value in MySQL?
- How to treat NULL as 0 and add columns in MySQL?
- How to convert MySQL null to 0 using COALESCE() function?

Advertisements