Mapping NULL Values to Other Values for Display : IF « Function « SQL / MySQL






Mapping NULL Values to Other Values for Display

      
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  name    CHAR(20),
    ->  id              CHAR(20)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO mytable (name,id) VALUES ('Tom','198-48');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable (name,id) VALUES ('Jack',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable (name,id) VALUES ('Mary',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable (name,id) VALUES ('Jane','475-83');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT * FROM mytable;
+------+--------+
| name | id     |
+------+--------+
| Tom  | 198-48 |
| Jack | NULL   |
| Mary | NULL   |
| Jane | 475-83 |
+------+--------+
4 rows in set (0.00 sec)

mysql>
mysql> SELECT name, IF(id IS NULL,'Unknown', id) AS 'id' FROM mytable;
+------+---------+
| name | id      |
+------+---------+
| Tom  | 198-48  |
| Jack | Unknown |
| Mary | Unknown |
| Jane | 475-83  |
+------+---------+
4 rows in set (0.00 sec)

mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
  








Related examples in the same category

1.The IF() function compares three expressions, as shown in the following syntax:
2.Use an IF function to evaluate which designs sell over 500 units during the winter sales quarter
3.SELECT IF('02-29' < '03-01','02-29','03-01') AS earliest;
4.IF(condition, result1, result2)
5.display GB value with IF() that maps 0 to a dash