List:General Discussion« Previous MessageNext Message »
From:Jon Stephens Date:November 18 2004 6:34am
Subject:Re: IF() problem
View as plain text  
A clarification. If a string value begins with a digit, MySQL does not 
convert it to zero (FALSE), but rather to an integer made up of the 
leading digits in the value (until a non-digit is reached). This can be 
shown by using the CAST() function.

mysql> SELECT '21b' = 0, CAST('21b' AS SIGNED);
+-----------+-----------------------+
| '21b' = 0 | CAST('21b' AS SIGNED) |
+-----------+-----------------------+
|         0 |                    21 |
+-----------+-----------------------+
1 row in set (0.00 sec)

However, if the string value begins with a non-digit, then it is coerced 
  or cast to zero:

mysql> SELECT 'b21' = 0, CAST('b21' AS SIGNED);
+-----------+-----------------------+
| 'b21' = 0 | CAST('b21' AS SIGNED) |
+-----------+-----------------------+
|         1 |                     0 |
+-----------+-----------------------+
1 row in set (0.00 sec)

It is still true that Przemyslaw was assuming that a non-empty string 
would always evaluate as TRUE, and this isn't necessarily the case. So 
the best strategy is to compare directly with the empty string, as 
already discussed.

Thanks to Paul DuBois for reminding me of this. My apologies to anyone 
whom I might have misled.

-- 
Jon Stephens, Technical Writer
MySQL AB   www.mysql.com
Office: +61 (07) 3388 2228
Are you MySQL certified?  www.mysql.com/certification
Thread
IF() problemPrzemyslaw Popielarski17 Nov
Re: IF() problemJon Stephens17 Nov
  • Re: IF() problemPrzemyslaw Popielarski18 Nov
Re: IF() problemJon Stephens18 Nov