
- 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 do I check if a column is empty or null in MySQL?
To check if a column is empty or null , we can use the where clause with IS NULL and for empty we can use the condition ‘ ‘ i.e. empty space. The steps required for this are as folllows: First a table is created with the help of create command as follows −
mysql> CREATE table ColumnValueNullDemo -> ( -> ColumnName varchar(100) -> ); Query OK, 0 rows affected (0.61 sec)
An empty value is inserted into the table using insert command. This is given below −
mysql> INSERT into ColumnValueNullDemo values(' '); Query OK, 1 row affected (0.14 sec)
After that, the table records are displayed using the select command. This is given as follows −
mysql> SELECT * from ColumnValueNullDemo;
After executing the above query, we will get the following output −
+-------------------+ | ColumnName | +-------------------+ | | +-------------------+ 1 row in set (0.00 sec)
To check if the column has null value or empty, the syntax is as follows −
SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' ';
The IS NULL constraint can be used whenever the column is empty and the symbol ( ‘ ‘) is used when there is empty value.
Now, the query using the above syntax is given as follows −
mysql> SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR ColumnName = ' ';
After executing the above query, the output obtained is.
+------------+ | ColumnName | +------------+ | | +------------+ 1 row in set (0.00 sec)
This output was obtained as the second condition is true for empty value.
Now, NULL value is inserted into the table with the help of the insert command as follows −
mysql> INSERT into ColumnValueNullDemo values(); Query OK, 1 row affected (0.14 sec)
The select command is used to view the contents of the table as follows −
mysql> SELECT * from ColumnValueNullDemo;
After executing the above query, the following output is obtained −
+------------+ | ColumnName | +------------+ | | | NULL | +------------+ 2 rows in set (0.00 sec)
Now, the condition to check for null or emptyis applied −
mysql> SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR ColumnName = '';
Both the table rows are obtained as output because it is true in both conditions.
+------------+ | ColumnName | +------------+ | | | NULL | +------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to check if field is null or empty in MySQL?
- Check whether a field is empty or null in MySQL?
- Check if a String is empty ("") or null in Java
- Check if a String is whitespace, empty ("") or null in Java
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- Check for NULL or NOT NULL values in a column in MySQL
- Check for NULL or empty variable in a MySQL stored procedure
- How do I modify a MySQL column to allow NULL?
- How do you check if a ResultSet is empty or not in JDBC?
- Empty string in not-null column in MySQL?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- Check if a table is empty or not in MySQL using EXISTS
- MySQL query to check if database is empty or not?
- Which one is better in MySQL - NULL or empty string?
