Forum Moderators: open

Message Too Old, No Replies

Sql Statement to Search between Two Dates

         

natalieo

1:34 pm on May 5, 2009 (gmt 0)

10+ Year Member



NEED HELP!
I have an Access database which contains the following fields.
OrderDate: Date Format
Customer Name: Text
Total:Currency
I would like to create a search page where I can search orders between two dates.
My page will give u the option to search by the following
CustomerName : Text Field
Orders From : Date Field
Order To: Date Field
E.g. Search All Customer Order from 1/20/2009 Order to 2/21/2009
My Results should show
CustomerName Order Dates
Client A 1/25/2009
Client B 1/29/2009
Client C 1/1/2009
Client D 1/1/2009
Client E 1/15/2009
What I need a SQL Statement that will create the search boxes and the results between the two dates.

coopster

1:57 pm on May 5, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, natalieo.

I cannot help you with the interactive interface (the form) but I can tell you that most databases have comparison functions and operators specifically for handling date comparison. The BETWEEN comparison is the likely solution in this case. Have a look in the Access Help for that keyword and hopefully there are examples.

LifeinAsia

4:05 pm on May 5, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Not sure why the orders from Clients C, D, & E would show in your example, since their order dates are NOT between 1/20/2009 and 2/21/2009.

rocknbil

5:03 pm on May 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard natalieo!

The following uses mysql queries for example, alter accordingly to be compatible with MSsql/Access (should be nearly identical.)

You write some routine/function that generates date lists in the programming language of your choice. So

$startList = date_list('start_date');
$endList = date_list('end_date');

So in your form, you just need to do:

$output .= "<strong>Orders between</strong> $startDate <strong>and:</strong> $endDate";

So the "date_list" function/subroutine would return three lists (each.) In the function, you append the select name with _day, _month, _year:

<select name="start_date_month" id="start_date_month">....
<select name="start_date_day" id="start_date_day">....
<select name="start_date_year" id="start_date_year">....

<select name="end_date_month" id="end_date_month">....
<select name="end_date_day" id="end_date_day">....
<select name="end_date_year" id="end_date_year">....

You would optimize the function by creating loops, for example, in the "day" section:

for ($i=1;$i<=31;$i++) {
$txtname=(length($i < 2))?'0'.$i:$i; // = 01, 02, 12, etc.
$list .= "<option value=\"$txtname\">$txtname</option>\n";
}

So when submitted, you have day, month, year for start date and end date. Almost done. You compile a mysql/MSsql-friendly variable:

(perl)
$start = qq¦$data{'start_date_year'}-$data{'start_date_month'}-$data{'start_date_day'}¦;
$end = qq¦$data{'end_date_year'}-$data{'end_date_month'}-$data{'end_date_day'}¦;

(PHP)
$start = "$_POST['start_date_year']-$_POST['start_date_month']-$_POST['start_date_day']";
$end = "$_POST['end_date_year']-$_POST['end_date_month']-$_POST['end_date_day']";

NOTE: Do not directly use input variables without cleansing first! Above for example only.

So now all you need to do is

$query = "select * from table where OrderDate >= '$start' and OrderDate <= '$end' order by OrderDate desc";

OR use between instead.

natalieo

3:29 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



Thanks