开发者

MySQL UNIX_TIMESTAMP for PostgreSQL

开发者 https://www.devze.com 2023-03-01 21:27 出处:网络
Basic开发者_高级运维ally 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

Basic开发者_高级运维ally 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


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


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.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消