SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: IFNULL function

  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    IFNULL function

    Hi, I have problem with the IFNULL function:

    http://dev.mysql.com/doc/refman/5.0/...unction_ifnull

    My code:
    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?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    1) Why use IFNULL, if you put NULL as first expression? It means it will always return the second expression.
    2) The error is not caused by IFNULL, but by STR_TO_DATE. It seems the value of myDate is NULL.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The documentation says

    Code:
    IFNULL(expr1,expr2)
    
    If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
    and gives an example

    Code SQL:
    mysql> SELECT IFNULL(NULL,10);
            -> 10

    So in your case

    Code:
    IFNULL(NULL, str_to_date(`myDate`, '%d/%m/%Y %H.%i')),
    expr2 is always evaluate even mydate is null

    You want the function

    Code:
    IF(expr1,expr2,expr3)
    
    If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.

  4. #4
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    2) The error is not caused by IFNULL, but by STR_TO_DATE. It seems the value of myDate is NULL.

    Yes, this is the problem... I have records where value of myDate is NULL...
    Any suggestion?
    Thanks-

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    use a CASE , or IF like Philip said.

  6. #6
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    use a CASE , or IF like Philip said.
    Ok:

    PHP Code:
    IF(str_to_date(`myDate`, '%d/%m/%Y %H.%i'),'0000-00-00',''
    [Err] 1411 - Incorrect datetime value: 'NULL' for function str_to_date


  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    IF(myDate IS NULL, something, something_else)

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Code:
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    IF(myDate IS NULL, something, something_else)
    PHP Code:
     IF(`myDateIS NULL'0000-00-00'str_to_date(`myDate`, '%d/%m/%Y %H.%i')) 
    [Err] 1411 - Incorrect datetime value: 'NULL' for function str_to_date



  10. #10
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    Code:
    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
    [Err] 1411 - Incorrect datetime value: 'NULL' for function str_to_date

  11. #11
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    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 !

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    So the value wasn't NULL, but 'NULL' (string value)

  13. #13
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    So the value wasn't NULL, but 'NULL' (string value)
    Are two values... NULL and 'NULL'...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •