
- 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 to convert MySQL null to 0 using COALESCE() function?
You can use the COALESCE() function to convert MySQL null to 0
SELECT COALESCE(yourColumnName,0) AS anyAliasName FROM yourTableName;
Let us first create a table. The query to create a table is as follows
mysql> create table convertNullToZeroDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20), -> Salary int -> ); Query OK, 0 rows affected (1.28 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into convertNullToZeroDemo(Name,Salary) values('John',NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into convertNullToZeroDemo(Name,Salary) values('Carol',5610); Query OK, 1 row affected (0.10 sec) mysql> insert into convertNullToZeroDemo(Name,Salary) values('Bob',NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into convertNullToZeroDemo(Name,Salary) values('David',NULL); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from convertNullToZeroDemo;
The following is the output
+----+-------+--------+ | Id | Name | Salary | +----+-------+--------+ | 1 | John | NULL | | 2 | Carol | 5610 | | 3 | Bob | NULL | | 4 | David | NULL | +----+-------+--------+ 4 rows in set (0.05 sec)
Here is the query to convert MySQL NULL to 0 using COALESCE() function
mysql> select coalesce(Salary,0) as `CONVERT_NULL _TO_0` from convertNullToZeroDemo;
The following is the output
+--------------------+ | CONVERT_NULL _TO_0 | +--------------------+ | 0 | | 5610 | | 0 | | 0 | +--------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Convert MySQL null to 0?
- What MySQL COALESCE() function returns if all the arguments provided to it are NULL?
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
- How can MySQL COALESCE() function be used with MySQL SUM() function to customize the output?
- How to use Coalesce in MySQL?
- Display first non-null values with coalesce() in MySQL?
- Typecasting NULL to 0 in MySQL
- How does COALESCE order results with NULL and NON-NULL values?
- What MySQL COALESCE() function returns if it has a blank, but not NULL, as the first argument?
- MySQL query to convert empty values to NULL?
- How to convert NaN to 0 using JavaScript?
- How to treat NULL as 0 and add columns in MySQL?
- Can MySQL automatically convert empty strings to NULL?
- How can I use IFNULL() function at the place of COALESCE() function in MySQL?
- How can we apply COALESCE() function on a MySQL table’s data value?

Advertisements