IF( ) and IFNULL( ) are useful for catching divide-by-zero operations : IFNULL « Function « SQL / MySQL






IF( ) and IFNULL( ) are useful for catching divide-by-zero operations

     
mysql>
mysql>
mysql> SET @hits = 0, @atbats = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @hits, @atbats, @hits/@atbats AS 'batting average';
+-------+---------+-----------------+
| @hits | @atbats | batting average |
+-------+---------+-----------------+
|     0 |       0 |            NULL |
+-------+---------+-----------------+
1 row in set (0.00 sec)

mysql>

   
    
    
    
    
  








Related examples in the same category

1.The IFNULL() function returns a value based on whether a specified expression evaluates to NULL.
2.IFNULL() tests its first argument and returns it if it's not NULL, or returns its second argument otherwise
3.Use IFNULL( ) to map their results appropriately:
4.IFNULL() takes two arguments. If the first argument is not NULL, that argument is returned; otherwise, the fun
5.SELECT ISNULL(NULL), ISNULL(0), ISNULL(1);
6.The following expression returns expr2 if expr1 is NULL, and expr1 otherwise: