
- 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 update empty string to NULL in MySQL?
For this, use LENGTH(), since if the length is 0 that would mean the string is empty. After finding, you can set it to NULL using the SET clause in the UPDATE command. Let us first create a table −
mysql> create table DemoTable ( Name varchar(50) ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Name | +-------+ | Chris | | | | David | | | +-------+ 4 rows in set (0.00 sec)
Following is the query to update empty string to NULL −
mysql> update DemoTable set Name=NULL where length(Name)=0; Query OK, 2 rows affected (0.12 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Name | +-------+ | Chris | | NULL | | David | | NULL | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- Empty string in not-null column in MySQL?
- How to test String is null or empty?
- Which one is better to insert NULL or empty string in MySQL?
- MySQL query to convert empty values to NULL?
- Which one is better in MySQL - NULL or empty string?
- How to check if field is null or empty in MySQL?
- Can MySQL automatically convert empty strings to NULL?
- MySQL query to replace null value with empty string in several columns while fetching data
- MySQL update column to NULL for blank values
- MySQL query to update a specific cell to be empty
- MySQL queries to update date records with NULL values
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- How to check empty/undefined/null strings in JavaScript?
- How do I update NULL values in a field in MySQL?

Advertisements