in reply to Trying to select all records that match a datetime range.
You appear to have some errors in $query. I think "$start_time" should be "$start_time_field" and "$end_time" "$end_time_field". Perhaps the logic is ok in your dbms, but I'd write that (using placeholders) as,
In what follows you may need to apply an epoch time conversion function to $start_time_field and $end_time_field. If your dbms is forgiving and helpful with datetime formats, the following may not be necessary.my $query = <<"ESQL"; SELECT * FROM $table WHERE $description_field LIKE '%$search_term%' and $start_time_field between ? and ? or $end_time_field between ? and ? ESQL
The surest way is to convert the dates and times to unix epoch time and do numerical comparisons. In Perl,
I'll put your start and end times into an array of (yr,mo,day,hr,min,sec) using a simpler regex,use Time::Local; # . . .
Now we can call timelocal() to get the range limits as epoch time,my @datetime_start = $date1 =~ /\d+/g; $#datetime_start = 5; # expand to six elements, # even if there are only three my @datetime_end = $date2 =~ /\d+/g; $#datetime_end = 5; # expand to six elements, # even if there are only three
All that remains is to compose the SQL query. You don't say what dbms you use, but most all will provide an epoch time conversion function for their native datetime type.my $datetime_start = timelocal reverse @datetime_start; my $datetime_end = timelocal reverse @datetime_end;
MySQL has a rich enough set of date and time functions that you don't need to jump through most of those hoops. Its comparison functions work fine with correctly formatted date strings. I think all you would need to do is $date1 =~ tr/()//d; and so on.
After Compline,
Zaxo
|
|---|