cms9651
1
Hi, I have problem with the IFNULL function:
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull
My code:
IFNULL(NULL, str_to_date(`myDate`, '%d/%m/%Y %H.%i')),
Error:
[Err] 1411 - Incorrect datetime value: ‘NULL’ for function str_to_date
Can you help me?
The documentation says
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
and gives an example
mysql> SELECT IFNULL(NULL,10);
-> 10
So in your case
IFNULL(NULL, str_to_date(`myDate`, '%d/%m/%Y %H.%i')),
expr2 is always evaluate even mydate is null
You want the function
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.
cms9651
4
Yes, this is the problem… I have records where value of myDate is NULL…
Any suggestion?
Thanks-
use a CASE , or IF like Philip said.
cms9651
6
Ok:
IF(str_to_date(`myDate`, '%d/%m/%Y %H.%i'),'0000-00-00','')
[Err] 1411 - Incorrect datetime value: ‘NULL’ for function str_to_date
IF(myDate IS NULL, something, something_else)
r937
8
CASE WHEN myDate IS NULL
THEN '1999-12-31' -- or whatever date you want
ELSE STR_TO_DATE(myDate,'%d/%m/%Y %H.%i') END
cms9651
9
IF(`myDate` IS NULL, '0000-00-00', str_to_date(`myDate`, '%d/%m/%Y %H.%i'))
[Err] 1411 - Incorrect datetime value: ‘NULL’ for function str_to_date
:sick:
cms9651
10
[Err] 1411 - Incorrect datetime value: ‘NULL’ for function str_to_date
cms9651
11
CASE WHEN `myDate` IS NULL OR `myDate` = 'NULL'
THEN '1999-12-31'
ELSE STR_TO_DATE(myDate,'%d/%m/%Y %H.%i') END
Great, thanks !
So the value wasn’t NULL, but ‘NULL’ (string value)
cms9651
13
Are two values… NULL and ‘NULL’…