adding dates, DATEADD function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rodrigo21
    New Member
    • Mar 2008
    • 20

    adding dates, DATEADD function

    Hello,

    I have one form with a field for entering a date 'orderdate' and another field for entering a days interval 'range'.
    This form is used to preform a query on a travel tickets database, so that I can search for tickets with dates from ('orderdate') till ('orderdate' + 'range').

    For achieving this I have the followiong code:
    [php]
    "SELECT * FROM viajes WHERE viajes.fecha BETWEEN DATEADD(Day,'". $_GET['rango'] ."','". $_GET['orderdate'] ."') AND '". $_GET['orderdate'] ."'";
    [/php]
    but Iam getting this error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Day,'4','2008-03-29') AND '2008-03-29' LIMIT 0, 10' at line 1

    I dont know if maybe I should use the mysql function DATE_ADD instead of sql DATEADD function,?

    Any help is welcome.

    note: at least the information from my form seems to be correctly coded in the query: 2008-03-29, as the error shows
    Last edited by ronverdonk; Mar 29 '08, 11:44 AM. Reason: code tags
  • write2ashokkumar
    New Member
    • Feb 2007
    • 39

    #2
    Hi, es, your query syntax is worng to add the date. So try with the following query.
    [code=mysql]
    SELECT *
    FROM viajes
    WHERE
    viajes.fecha BETWEEN orderdate AND DATE_ADD(orderd ate,INTERVAL rango DAY);[/code]
    Here,
    [php]rango = $_GET['rango']
    orderdate = $_GET['orderdate'][/php]
    So u can format the query as per the PHP concatination syntax.
    For general, date add function

    Syntax:
    [code=mysql]DATE_ADD(date,I NTERVAL expr unit);[/code]
    Example:[code=mysql]SELECT DATE_ADD(DATE(N OW()),INTERVAL 2 DAY) AS DT;[/code]I hope.. this will help you...
    Good Luck...

    Regards,
    S.Ashokkumar
    Last edited by ronverdonk; Mar 29 '08, 11:47 AM. Reason: code tags (again)!!

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Please enclose your posted code in [code] tags (See How to Ask a Question).

      This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

      Please use [code] tags in future.

      MODERATOR

      Comment

      • rodrigo21
        New Member
        • Mar 2008
        • 20

        #4
        Originally posted by write2ashokkuma r
        Hi, es, your query syntax is worng to add the date. So try with the following query.
        [code=mysql]
        SELECT *
        FROM viajes
        WHERE
        viajes.fecha BETWEEN orderdate AND DATE_ADD(orderd ate,INTERVAL rango DAY);[/code]
        Here,
        [php]rango = $_GET['rango']
        orderdate = $_GET['orderdate'][/php]
        So u can format the query as per the PHP concatination syntax.
        For general, date add function

        Syntax:
        [code=mysql]DATE_ADD(date,I NTERVAL expr unit);[/code]
        Example:[code=mysql]SELECT DATE_ADD(DATE(N OW()),INTERVAL 2 DAY) AS DT;[/code]I hope.. this will help you...
        Good Luck...

        Regards,
        S.Ashokkumar
        Ok thanks a lot, it works now. This is the code I use:

        [code=mysql]"SELECT * FROM viajes WHERE viajes.fecha BETWEEN '". $_GET['orderdate'] ."' AND DATE_ADD('". $_GET['orderdate'] ."',INTERVAL '". $_GET['rango'] ."' day)";[/code]

        Could you tell me why I had to use the DATE_ADD instead of the DATEADD function? I think I am lost in between SQL and MYSQL

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          ....Could you tell me why I had to use the DATE_ADD instead of the DATEADD function? I think I am lost in between SQL and MYSQL
          DATEADD does not exist as a MySQL function, DATE_ADD is the correct one.

          Btw rodrigo21: what is your comment on the reply to a previous thread of yours at this link http://www.thescripts.com/forum/post3115825-16.html ?

          Ronald

          Comment

          Working...