SQL NULL values and how to check them
In Sql NULL value means no value at all. If any column which is optional in database is inserted or updated with no value then we can say that that filed has NULL value. NULL value is different from 0 or space.
IS NULL syntax and example in MySql -
Suppose we want to get the result set where AGE is null, we will use the following syntax.
Suppose we want to get the result set where AGE is not null, we will use the following syntax.
+----+--------+---------+------+-----------+ | ID | FNAME | LNAME | AGE | COUNTRY | +----+--------+---------+------+-----------+ | 1 | AKSHAY | KUMAR | 12 | INDIA | | 2 | JONTY | RHODES | 40 | CHINA | | 3 | JOHNY | DEPP | 45 | INDIA | | 4 | JESSIE | PINKMAN | 25 | AUSTRALIA | | 5 | JESSIE | JACKSON | 22 | CANADA | | 6 | NICOLE | KIDMAN | 50 | CANADA | | 7 | GEORGE | CLOONEY | NULL | USA | +----+--------+---------+------+-----------+ 7 rows in set (0.00 sec)
Checking for NULL values in MySql - IS NULL and IS NOT NULL
NULL values cannot be checked with comparison operators with where clause.IS NULL syntax and example in MySql -
Suppose we want to get the result set where AGE is null, we will use the following syntax.
SELECT * FROM CUSTOMER WHERE AGE IS NULL;
+----+--------+---------+------+---------+ | ID | FNAME | LNAME | AGE | COUNTRY | +----+--------+---------+------+---------+ | 7 | GEORGE | CLOONEY | NULL | USA | +----+--------+---------+------+---------+ 1 row in set (0.00 sec)IS NOT NULL syntax and example in MySql -
Suppose we want to get the result set where AGE is not null, we will use the following syntax.
SELECT * FROM CUSTOMER WHERE AGE IS NOT NULL;
+----+--------+---------+------+-----------+ | ID | FNAME | LNAME | AGE | COUNTRY | +----+--------+---------+------+-----------+ | 1 | AKSHAY | KUMAR | 12 | INDIA | | 2 | JONTY | RHODES | 40 | CHINA | | 3 | JOHNY | DEPP | 45 | INDIA | | 4 | JESSIE | PINKMAN | 25 | AUSTRALIA | | 5 | JESSIE | JACKSON | 22 | CANADA | | 6 | NICOLE | KIDMAN | 50 | CANADA | +----+--------+---------+------+-----------+ 6 rows in set (0.00 sec)
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment