Search code examples
node.jspostgresqlprepared-statement

Looking for a way to parameterize a PSQL interval


I have the following code snippet working as intended in PSQL:

SELECT * 
FROM Puzzle 
WHERE date > current_date - interval '1' year 
  AND date < current_date + interval '50' day 

I am now running into an issue when parameterizing this in Node.js. My best guess is below, but this returns an error on the $1

client.query('
SELECT * 
FROM Puzzle 
WHERE date > current_date - interval $1 year 
  AND date < current_date + interval $2 day
', ['1', '50'])

Any and all help appreciated!


Solution

  • You can use integer parameters if you rephrase the query a bit. For example:

    SELECT * 
    FROM Puzzle 
    WHERE date > current_date - $1 * 'interval 1 year'
      AND date < current_date + $2 * 'interval 1 day'