DBI select 'like' query

C

ccc31807

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.
 
C

ccc31807

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
 
K

krakle

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.
=)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top