I'm working server-side (using SSH) in psql, where I set variables using the '\set' command.
My concern is this: I have a query where I'm inserting a set string (date) into a datetime, but I get thrown a syntax error.
Simple example:
SELECT
network_no,
program_no,
national_datetime.
FROM
views
WHERE
national_datetime
between ':from_date 06:00:00'
and ':to_date 05:59:59'::timestamp + '1 day'::interval
Where 'from_date' and 'to_date' have been set accordingly:
\set from_date 2017-07-10
\set to_date 2017-07-16
I know normally with dates you're supposed to set three sets of apostrophes to read it in correctly (I never looked up the reason), but I'm feeding this date into a datetime, so I figured using no apostrophes would work, but I get thrown this error:
ERROR: invalid input syntax for type timestamp: ":from_date 06:00:00"
LINE 40: between ':from_date 06:00:00'
Normally in a bash script this would work since it just passes the literal string value, but in this case, whether I put no or one set of apostrophes around the date values it won't pass the value (I'm assuming because of the way PSQL handles set variables.
I know there's ways around this, but I'm looking for a reason why something like this would be happening, and whether there's a simple way to fix the "invalid input syntax"--whether it be through casting a variable, setting the variable differently, etc.
Many thanks in advance!