- 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
Return 0 in a new column when record is NULL in MySQL?
For this, you can use CASE statement. Let us first create a table −
mysql> create table DemoTable703 (Price int); Query OK, 0 rows affected (0.46 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable703 values(102); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable703 values(null); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable703 values(0); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable703 values(500); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable703 values(100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable703 values(null); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable703 values(2340); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable703;
This will produce the following output -
+-------+ | Price | +-------+ | 102 | | NULL | | 0 | | 500 | | 100 | | NULL | | 2340 | +-------+ 7 rows in set (0.00 sec)
Use CASE statement −
mysql> select Price, case When Price IS NULL Then 0 else Price END AS Result from DemoTable703;
This will produce the following output -
+-------+--------+ | Price | Result | +-------+--------+ | 102 | 102 | | NULL | 0 | | 0 | 0 | | 500 | 500 | | 100 | 100 | | NULL | 0 | | 2340 | 2340 | +-------+--------+ 7 rows in set (0.00 sec)
- Related Articles
- Return null for date_format when input is null in MySQL?
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- How can I return 0 for NULL in MySQL?
- Display custom text in a new column on the basis of null values in MySQL?
- Concatenate two columns when one of such column values is null in MySQL
- Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL
- Typecasting NULL to 0 in MySQL
- Replace 0 with null in MySQL?
- Replace a specific duplicate record with a new value 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
- MySQL: selecting rows where a column is null?
- Insert default into not null column if value is null in MySQL?
- What happens when I insert the value ‘NULL’ in an AUTO_INCREMENT MySQL column?
- When MySQL IN() function returns NULL?

Advertisements