469,572 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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
Mar 29 '08 #1
4 17122
Hi, es, your query syntax is worng to add the date. So try with the following query.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM     viajes 
  3. WHERE     
  4.     viajes.fecha  BETWEEN orderdate AND DATE_ADD(orderdate,INTERVAL rango DAY);
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:
Expand|Select|Wrap|Line Numbers
  1. DATE_ADD(date,INTERVAL expr unit);
Example:
Expand|Select|Wrap|Line Numbers
  1. SELECT DATE_ADD(DATE(NOW()),INTERVAL 2 DAY) AS DT;
I hope.. this will help you...
Good Luck...

Regards,
S.Ashokkumar
Mar 29 '08 #2
ronverdonk
4,258 Expert 4TB
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
Mar 29 '08 #3
Hi, es, your query syntax is worng to add the date. So try with the following query.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM     viajes 
  3. WHERE     
  4.     viajes.fecha  BETWEEN orderdate AND DATE_ADD(orderdate,INTERVAL rango DAY);
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:
Expand|Select|Wrap|Line Numbers
  1. DATE_ADD(date,INTERVAL expr unit);
Example:
Expand|Select|Wrap|Line Numbers
  1. SELECT DATE_ADD(DATE(NOW()),INTERVAL 2 DAY) AS DT;
I hope.. this will help you...
Good Luck...

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

Expand|Select|Wrap|Line Numbers
  1. "SELECT * FROM viajes WHERE viajes.fecha BETWEEN '". $_GET['orderdate'] ."' AND DATE_ADD('". $_GET['orderdate'] ."',INTERVAL '". $_GET['rango'] ."' day)";
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
Mar 29 '08 #4
ronverdonk
4,258 Expert 4TB
....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
Mar 29 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Zlatko Matić | last post: by
2 posts views Thread by Abdul N K | last post: by
4 posts views Thread by ey.markov | last post: by
1 post views Thread by C.Davidson | last post: by
reply views Thread by Zlatko Matić | last post: by
2 posts views Thread by Rich Raffenetti | last post: by
3 posts views Thread by Mel | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.