Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with parameterizing timestamp references in PostgreSQL in Perl
    primarykey
    data
    text
    <p>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.</p> <p>I've defined <code>$start_date</code> and <code>$end_date</code> as timestamps and intervals:</p> <pre><code>my $start_date = "current_timestamp - interval '6 hours'"; my $end_date = "current_timestamp"; </code></pre> <p>I use the following to submit to the database, with <code>$dbh</code> defined earlier:</p> <pre><code>my $sql = "SELECT cast(? as timestamp), cast(? as timestamp)"; my $sth = $dbh-&gt;prepare($sql); $sth-&gt;execute($start_date, $end_date); </code></pre> <p>When I do this, I get a somewhat confusing error.</p> <pre><code>DBD::Pg::st execute failed: ERROR: date/time value "current" is no longer supported </code></pre> <p>I understand that <code>current</code> hasn't been supported in PG since 7.2, but I'm not using that. I'm using <code>current_timestamp</code>, which <em>is</em> supported, AFACT. To wit, if I enter into <code>psql</code>:</p> <pre><code>select (cast(current_timestamp - interval '6 hours' as timestamp), cast(current_timestamp as timestamp)); </code></pre> <p>the result is what I expect (two timestamps, the former six hours previous to the latter).</p> <p>I could also use <code>now()</code> rather than <code>current_timestamp</code>. I can use it in the following way:</p> <pre><code>my $start_date = "now() - interval '6 hours'"; my $end_date = "now()"; </code></pre> <p>When I try to run the query in perl, I get the following error:</p> <pre><code>DBD::Pg::st execute failed: ERROR: invalid input syntax for type timestamp: "now() - interval '6 hours'" </code></pre> <p>Yet, the query:</p> <pre><code>select (cast(now() - interval '6 hours' as timestamp), cast(now() as timestamp)); </code></pre> <p>gives me the expected result.</p> <p>I am quite flummoxed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload