DBI select 'like' query

Discussion in 'Perl Misc' started by ccc31807, Mar 10, 2009.

  1. ccc31807

    ccc31807 Guest

    Running MySQL. I have a column in my table called 'eventdate' of type
    date with values that look like '2009-03-10'. I want to query by year
    and month only.

    If I run this query: "SELECT * FROM EVENTS WHERE eventdate LIKE
    '2009-03%';" I get all March, 2009 events.

    However, I can't seem to find the correct syntax to do this:

    sub get_events_by_month
    {
    my $d = shift; # a value like '2009-03'
    $dbh = con(); # internal function that connects to DB
    $sth = $dbh->prepare("SELECT * FROM EVENTS WHERE eventdate LIKE ?
    %");
    $sth->execute($t);
    $hash = $sth->fetchall_hashref('id');
    $sth->finish();
    $dbh->disconnect();
    return $hash;
    }

    I've tried a number of different statements, some using bind_param,
    with no luck.

    Suggestions? Thanks, CC.
    ccc31807, Mar 10, 2009
    #1
    1. Advertising

  2. ccc31807

    ccc31807 Guest

    On Mar 10, 12:14 pm, Ben Morrow <> wrote:
    > Did you try
    >
    >         my $sth = $dbh->prepare(
    >             "SELECT * FROM EVENTS WHERE eventdate LIKE ?"
    >         );
    >         $sth->execute("$t%");
    >
    > ?


    Man, I spent the last two hours trying every permutation I could think
    of, getting errors, no errors, no results, no success.

    Okay, I'll confess to ignorance. Yes, this works. Thanks.

    CC
    ccc31807, Mar 10, 2009
    #2
    1. Advertising

  3. Petr Vileta "fidokomik" <> wrote:

    > sub get_events_by_month
    > {
    > my $d = shift; # a value like '2009-03'

    ^
    ^
    > my ($year, $month) = split(/_/, $d);

    ^
    ^
    Oops.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Mar 11, 2009
    #3
  4. ccc31807

    Guest

    On Mar 10, 11:57 am, ccc31807 <> wrote:
    > If I run this query: "SELECT * FROM EVENTS WHERE eventdate LIKE
    > '2009-03%';" I get all March, 2009 events.


    Not a Perl response but...

    if eventdate it a DATETIME column type there are better ways to fetch
    all records from March, 2009. You may want to look up mySQL's date
    functions... It'll save you a lot of headachs and work in the future.
    =)
    , Mar 11, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulloa
    Replies:
    1
    Views:
    512
    Juha Laiho
    Jul 22, 2004
  2. Mavis
    Replies:
    1
    Views:
    594
    Andres Monroy-Hernandez
    Oct 3, 2004
  3. Jerome Hauss
    Replies:
    0
    Views:
    159
    Jerome Hauss
    Oct 13, 2004
  4. Asby

    Mason, DBI, and DBI::Pg

    Asby, Jul 24, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    166
  5. Tim Haynes
    Replies:
    3
    Views:
    129
    Ron Reidy
    Sep 13, 2003
Loading...

Share This Page