
- 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 the NULL values to 0 and display the entire column in a new column with MySQL SELECT
For this, use IFNULL(). Let us first create a table −
mysql> create table DemoTable (Value int); Query OK, 0 rows affected (1.02 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | NULL | | 10 | | 20 | | NULL | | 30 | +-------+ 5 rows in set (0.00 sec)
Set NULL values to 0 −
mysql> select Value,IFNULL(Value,0) from DemoTable;
This will produce the following output −
+-------+-----------------+ | Value | IFNULL(Value,0) | +-------+-----------------+ | NULL | 0 | | 10 | 10 | | 20 | 20 | | NULL | 0 | | 30 | 30 | +-------+-----------------+ 5 rows in set (0.00 sec)
- Related Articles
- How to round MySQL column with float values and display the result in a new column?
- Display custom text in a new column on the basis of null values in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Extract the middle part of column values in MySQL surrounded with hyphens and display in a new column?
- Compare for NULL value and display value 1 for these values in a new MySQL column?
- Select distinct values from three columns and display in a single column with MySQL
- Return 0 in a new column when record is NULL in MySQL?
- Concatenate the column values with separate text in MySQL and display in a single column
- MySQL select to count values equal to 0 and greater than 0 from a column?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to display only the column values with corresponding column having whitespace
- Add a character in the end to column values with MySQL SELECT?
- Multiply values of two columns and display it a new column in MySQL?
- How to set NULL values in a table column and insert the same
- How to select different values from same column and display them in different columns with MySQL?

Advertisements