MySQL UNIX_TIMESTAMP for PostgreSQL

烈酒焚心 提交于 2020-02-02 03:05:49

问题


Basically I am trying to convert a date field from my database into a number of seconds old. I used UNIX_TIMESTAMP on our old MySQL database and am looking for an equivalent function in PostgreSQL.

Thanks in advance


回答1:


SELECT extract(epoch FROM your_datetime_column)
FROM your_table

More details in the manual:

http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT




回答2:


This:

select CURRENT_TIMESTAMP - '1970-01-01';

Will give you an interval type. You can get seconds out of that interval result if you need it.




回答3:


I am trying to convince existing MySQL queries to work, this is what I have, seem to be working fine to me.

CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(date_field timestamp with time zone) RETURNS integer 
AS $$
BEGIN
    RETURN extract(epoch FROM date_field);
END;
$$ LANGUAGE plpgsql;


来源:https://stackoverflow.com/questions/5857990/mysql-unix-timestamp-for-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!