JungleBoy has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a problem formatting the current date so that I can use it in an SQL query. Below is a snippet of my code. How do I get it properly formatted, or is there another way to do this entirely?
my $cur_time = localtime(); $sth = $dbh->prepare("UPDATE Schedules SET Date = '$cur_time' WHERE ID + = $id") or die "Couldn't prepare statement: $DBI::errstr; stopped";

Replies are listed 'Best First'.
Re: Dates and SQL
by Zaxo (Archbishop) on Sep 27, 2001 at 05:36 UTC

    use Date::Format;Its perldoc is concise and tells all. Or, if the column is a timestamp field, just write NULL to it

    Update: Re: OP reply; Check your database server or DBI driver docs to see what format SQL wants to see. "%c" gives ctime format, which the server may not use. Also, your driver may want $dbi->quote() applied to the data. Placeholder syntax would let you skip quoting and gain efficiency if you do several updates in a run.

    After Compline,
    Zaxo

      I've tried this, unfortunately I get a "Syntax error in UPDATE statement."
      Any other ideas?
      This is what it looks like now:
      my $cur_time = time2str("%c", time()); $sth = $dbh->prepare("UPDATE Schedules SET Date = '$cur_time' WHERE ID + = $id") or die "Couldn't prepare statement: $DBI::errstr; stopped";
        I looks like that your Database doesn't recognise the string as a date. Check what it does accept and format your date accordingly. If you don't have any documentation select back a date and look at how it is formatted. It will most probably accept that as an input
        For easy date formatting I often use the POSIX module's strftime routine to format the date. e.g.
        use POSIX qw(strftime); my $cur_time = strftime "%m %d %Y", localtime;
Re: Dates and SQL
by htoug (Deacon) on Sep 27, 2001 at 11:10 UTC
    My database system (Ingres) allows me to use the constant 'NOW' as the current date. You query would then become:
    $sth = $dbh->prepare("UPDATE Schedules SET Date = 'NOW' WHERE ID = $id +") or die "Couldn't prepare statement: $DBI::errstr; stopped";
    Check the database documentation and see if a similar method exists.

    If not then you can fudge the date using localtime in array mode and a bit of sprintf:

    my @cur_time = localtime(); my $now = sprintf("%4d-%02d-%02d", $cur_time[5]+1900, $cur_time[4]+1, +$cur_time[3]);
    for an ISO standard date.

    You are using $dbh->{RaiseError}=1, aren't you? It does make error-checking so much easier - you don't need all those or die "Error...." check everywhere. And you don't forget the one where the code fails in production!

Re: Dates and SQL
by ducky (Scribe) on Sep 27, 2001 at 11:06 UTC

    I've got a couple of suggestions for you:

    1. localtime? - Perhaps you might be interested in something you have a little more direct control over - Date::Format maybe?
    2. prepare? How about do - When you expect to get rows out of your SQL statement one option is to prepare a Statement Handle Object first. Since you're doing an UPDATE, you really shouldn't be getting any rows back. Try
      my $cur_time = time2str("%c", time) ; $dbh->do("UPDATE Schedules SET Date = ? WHERE ID = ?", undef, $cur_time, $id) ;
      Errors may be caught in $dbh->errstr
    3. Placeholders - In the 2nd item I use question marks as placeholders. The format for do is $dbh->do($statement, \%attr, @bind_values);. You'll probably not need attrs, so undef should suffice. Placeholders are great because they make it easy to read the SQL and DBI will do the escaping of the values for you.
    4. Using prepare - If you still would like to use prepare I'd recommended using placeholders:
      my $sth = $dbh->prepare("UPDATE Schedules SET Date = ? WHERE ID = ?"); my $cur_time = time2str("%c", time) ; $sth->execute($cur_time, $id) ;
      This may be prefered if you're going to be executing that $sth in a few places. Once $sth is prepared, it may be executed with different values multiple times.

    Hope that helps =)

    Update: BTW, I don't know what $id looks like, but if it's non-numeric, that's probably what UPDATE is barfing on: Needs escaping... but placeholders take care of that stuff =)

    -Ducky

Re: Dates and SQL
by ralphie (Friar) on Sep 27, 2001 at 21:44 UTC
    you may need to check the sql reference for your database. when doing date queries in postgresql, for example, i've had to do something like "where to_date($date,"MM-DD-YYYY") > field_name" ...