
- 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
Treat a MySQL column field as NULL if it is blank?
Let us first create a table −
mysql> create table DemoTable1362 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientName varchar(40) -> ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1362(ClientName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1362(ClientName) values(' '); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1362(ClientName) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1362(ClientName) values(' '); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1362(ClientName) values('David'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1362(ClientName) values(NULL); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1362;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | Chris | | 2 | | | 3 | Bob | | 4 | | | 5 | David | | 6 | NULL | +----------+------------+ 6 rows in set (0.00 sec)
Following is the query to treat a column field as null if it is blank −
mysql> update DemoTable1362 -> set ClientName=NULL -> where ClientName='' or length(ClientName)=0; Query OK, 2 rows affected (0.21 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1362;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 1 | Chris | | 2 | NULL | | 3 | Bob | | 4 | NULL | | 5 | David | | 6 | NULL | +----------+------------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL update column to NULL for blank values
- What MySQL COALESCE() function returns if it has a blank, but not NULL, as the first argument?
- How to update a field with a particular value if it is null in MySQL?
- How to treat NULL as 0 and add columns in MySQL?
- Select a field and if it's null, select another with MySQL?
- Insert default into not null column if value is null in MySQL?
- MySQL query that returns a specific string if column is null?
- How to check if field is null or empty in MySQL?
- Select different fields in MySQL even if a field is set to null?
- Only update the MySQL field if the field contains null or 0?
- How to select ID column as null in MySQL?
- Update a column A if null, else update column B, else if both columns are not null do nothing with MySQL
- MySQL: selecting rows where a column is null?
- How do I check if a column is empty or null in MySQL?
- How it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?

Advertisements