I am trying to use a parameterized query in a Perl script to get some timestamps back from a Postgres database. Here's a cut-and-dried example, solely for pedagogical purposes.
I've defined $start_date and $end_date as timestamps and intervals:
my $start_date = "current_timestamp - interval '6 hours'";
my $end_date = "current_timestamp";
I use the following to submit to the database, with $dbh defined earlier:
my $sql = "SELECT cast(? as timestamp), cast(? as timestamp)";
my $sth = $dbh->prepare($sql);
$sth->execute($start_date, $end_date);
When I do this, I get a somewhat confusing error.
DBD::Pg::st execute failed: ERROR: date/time value "current" is no longer supported
I understand that current hasn't been supported in PG since 7.2, but I'm not using that. I'm using current_timestamp, which is supported, AFACT. To wit, if I enter into psql:
select (cast(current_timestamp - interval '6 hours' as timestamp), cast(current_timestamp as timestamp));
the result is what I expect (two timestamps, the former six hours previous to the latter).
I could also use now() rather than current_timestamp. I can use it in the following way:
my $start_date = "now() - interval '6 hours'";
my $end_date = "now()";
When I try to run the query in perl, I get the following error:
DBD::Pg::st execute failed: ERROR: invalid input syntax for type timestamp: "now() - interval '6 hours'"
Yet, the query:
select (cast(now() - interval '6 hours' as timestamp), cast(now() as timestamp));
gives me the expected result.
I am quite flummoxed.
prepare, and then I'm usingexecuteto run it with the appropriate parameters. (I'm getting this both from existing (working) code in our codebase, along with the DBI documentation.)$startand$endto be arbitrary pg expressions of timestamps? Or will they always be offsets from "now"? (And, if so, will the offsets always be in hours?)