FROM_UNIX alternate in PostgreSQL

北城以北 提交于 2019-12-10 14:31:35

问题


Guys I have stored unix timestamps in my MySQL database, where I was reading them using the MySQL function FROM_UNIX().

Now I'm migrating the database from MySQL to PostgreSQL. In PostgreSQL, how I can read unix timestamps just like I was doing in MySQL with FROM_UNIX() ?


回答1:


Taken from the manual:

to_timestamp(double precision) convert Unix epoch to time stamp

If you need parts of the created timestamp, use the extract function

select extract(year from to_timestamp(1284352323))



回答2:


SELECT to_char(date(to_timestamp(1195374767)),'YYYY-MM-DD');
  • to_timestamp - convert to Postgresql timestamp no unix timestamp
  • date convert to date type
  • to_char format output

http://www.postgresql.org/docs/8.1/static/functions-formatting.html

extract can do the same thing but not in one step



来源:https://stackoverflow.com/questions/12194416/from-unix-alternate-in-postgresql

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