I want to write a function that returns a table with all the rows between firstDate
and lastDate
. The rows have datatype timestamp without time zone They also have to be of a specific node id.
This is my function:
CREATE OR REPLACE FUNCTION get_measurements_by_node_and_date(nodeID INTEGER, firstDate date, lastDate date)
RETURNS TABLE (measurement_id INTEGER, node_id INTEGER, carbon_dioxide DOUBLE PRECISION,
hydrocarbons DOUBLE PRECISION, temperature DOUBLE PRECISION,
humidity DOUBLE PRECISION,
air_pressure DOUBLE PRECISION,
measurement_timestamp timestamp without time zone ) AS
$$
DECLARE
sql_to_execute TEXT;
BEGIN
SELECT 'SELECT measurements_lora.id,
measurements_lora.node_id,
measurements_lora.carbon_dioxide,
measurements_lora.hydrocarbons,
measurements_lora.temperature,
measurements_lora.humidity,
measurements_lora.air_pressure,
measurements_lora.measurement_timestamp AS measure
FROM public.measurements_lora
WHERE measurements_lora.measurement_timestamp <= '||lastDate||'
AND measurements_lora.measurement_timestamp >= '||firstDate||'
AND measurements_lora.node_id = '||nodeID||' '
INTO sql_to_execute;
RETURN QUERY EXECUTE sql_to_execute;
END
$$ LANGUAGE plpgsql;
The column measurement_timestamp is of type timestamp without time zone and is formatted like yy-mm-dd hh-mm-ss
When I run SELECT * FROM get_measurements_by_node_and_date(1, '2020-5-1', '2020-5-24')
I get the following error:
ERROR: operator does not exist: timestamp without time zone <= integer LINE 10: ... WHERE measurements_lora.measurement_timestamp <= 2020-05...
I don't get why it says "integer", because I clearly defined firstDate
and lastDate
as type date
.