I have a sql_query plugin for a irc bot. It will perform preconfigured sql queries, I want to extend it so users can specify a time range and query. I need to match time range provided by a user to a series of change records. It needs to match any changes start_time through end_time to the user provided range. The solution may be more of a sql issue. What I have now (not completely tested) will match any change that has a start_time or end_time w/in the user provided range but it will miss any records that both start and end out side the range provided by the user. Input is expected to be in this format () = optional: yr-mo-dd (hr:min:sec) - yr-mo-dd (hr:min:sec) search term (Updated) Using MySql and DBI. Great responses, thanks.
$start_time_field='start_time'; $end_time_field='end_time'; $search = "yr-mo-dd (hr:min:sec) - yr-mo-dd (hr:min:sec) search term"; trim($search); # removes white space and non printables my $date_field_start = $public_handlers{$cmd}->{'args'}->{'date_field_ +start'}; my $date_field_end = $public_handlers{$cmd}->{'args'}->{'date_field_en +d'}; my $description_field = $public_handlers{$cmd}->{'args'}->{'descriptio +n_field'}; ($date1,$date2)=split / - /, $search; ($datetime_start) = $date1 =~ /(\d\d-\d\d-\d\d \(\d\d;\d\d:\d\d\))|(\d +\d-\d\d-\d\d)/; ($datetime_end) = $date2 =~ /(\d\d-\d\d-\d\d \(\d\d;\d\d:\d\d\))|(\d\d +-\d\d-\d\d)/; ($search_term) = $date2 =~ /\d\d-\d\d-\d\d \(\d\d;\d\d:\d\d\)(.+)|\d\d +-\d\d-\d\d (.+)/; if ($datetime_start !~ /\d\d:\d\d:\d\d/) {$datetime_start = $datetime_ +start . " 00:00:00"}; if ($datetime_end !~ /\d\d:\d\d:\d\d/) {$datetime_end = $datetime_end +. " 23:59:59"}; $query = "SELECT * FROM $table WHERE $description_field LIKE '%$search +_term%' and $start_time_field or $end_time_field between '$datetime_s +tart' and '$datetime_end' ";

In reply to Trying to select all records that match a datetime range. by swares

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.