SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to use IF statements in SQL code

    I have the code below I want to figure out how to use an IF statement in the SQL code. I am using MySQL 3.xx.

    Basically the statement I want write asks if dateEnded = '0000-00-00', I want to substitute today's date. If it's not then use the value for dateEnded.

    The code below throws an error:
    [Local Server] ERROR 1064: You have an error in your SQL syntax near 'DATE_FORMAT(dateEnded, '%Y-%m-%d') = '0000-00-00' THEN CURDATE() ELSE dateEnded

    Thanks for your help.


    SELECT name, DATE_FORMAT(dateStarted, '%Y-%m-%d'),
    IF DATE_FORMAT(dateEnded, '%Y-%m-%d') = '0000-00-00' THEN CURDATE() ELSE dateEnded
    FROM table1

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THEN isn't a keyword in PHP.

    PHP Code:
     if (DATE_FORMAT(...) = '0000-00-00')
     {
     return 
    CURDATE();
     }
     else
     {
     return 
    dateEnded();
     } 
    Check the php manual at php.net for more info on dates, etc.

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're right but I actually JUST NOW found a MYSQL equivalent:

    IF (a=b, c, d)

    http://dev.mysql.com/doc/mysql/en/co...functions.html


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
  •