3

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?

1
  • Unless you really need the fractional part of the seconds, I would make the return type an INTEGER!
    – Vérace
    Commented Jul 20, 2020 at 3:09

1 Answer 1

1

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.