Postgresql – Using Postgres param in a function

functionspostgresqltimestamp

I'm trying to create a few functions to make the SQL queries between my SQLite and Postgres DB's the same.

One thing I would like to do is change a timestamp to a double via a function such as:

create function tz_to_double(a timestamp) returns DOUBLE PRECISION as $$
    SELECT EXTRACT(EPOCH FROM TIMESTAMP a);
$$ language sql;

Postgres however doesn't allow me to create this function. It errors out like so:

ERROR:  syntax error at or near "a"
LINE 2:  SELECT EXTRACT(EPOCH FROM TIMESTAMP a);

Would it be possible to create this function / use this as a param?

Best Answer

You are adding both Timestamp and a in extract function, just remove TIMESTAMP

create function tz_to_double(a timestamp) returns DOUBLE PRECISION as $$
    SELECT EXTRACT(EPOCH FROM  a);
$$ language sql;