
- 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
Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
Let us first create a table −
mysql> create table DemoTable839( StudentFirstName varchar(100), StudentLastName varchar(100) ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable839 values('Chris','Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable839 values('','Taylor'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable839 values(NULL,'Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable839 values('Adam','Smith'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable839;
This will produce the following output −
+------------------+-----------------+ | StudentFirstName | StudentLastName | +------------------+-----------------+ | Chris | Brown | | | Taylor | | NULL | Taylor | | Adam | Smith | +------------------+-----------------+ 4 rows in set (0.00 sec)
Following is the query to return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values−
mysql> select if(length(StudentFirstName),StudentFirstName,StudentLastName) from DemoTable839;
This will produce the following output −
+---------------------------------------------------------------+ | if(length(StudentFirstName),StudentFirstName,StudentLastName) | +---------------------------------------------------------------+ | Chris | | Taylor | | Taylor | | Adam | +---------------------------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Looping in JavaScript to count non-null and non-empty values
- GROUP BY and display only non-empty column values in MySQL
- MySQL query to display only the empty and NULL values together?
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- Query non-empty values of a row first in ascending order and then display NULL values
- How MySQL handles the empty and null values for enumerations?
- Update all the fields in a table with null or non-null values with MySQL
- Need help selecting non-empty column values from MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Fetch maximum value from multiple columns with null and non-null values?
- How does COALESCE order results with NULL and NON-NULL values?
- MySQL query to convert empty values to NULL?
- Display first non-null values with coalesce() in MySQL?
- MySQL query to replace only the NULL values from the table?
- From a list of IDs with empty and non-empty values, retrieve specific ID records in JavaScript

Advertisements