MySQL postgresql 中的 DATE_FORMAT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12052705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:33:02  来源:igfitidea点击:

DATE_FORMAT in postgresql

mysqldatabasepostgresqlpostgresql-9.1postgresql-9.0

提问by Sathish

I'm working in postgresql and I need to convert the date format in query itself, in mysql there is option called DATE_FORMATand I can use a query like this:

我在 postgresql 中工作,我需要在查询本身中转换日期格式,在 mysql 中有调用的选项DATE_FORMAT,我可以使用这样的查询:

Select DATE_FORMAT(date_time, '%b %e, %Y, %T') from table_name

is there any option in postgresql? Please let me know if any?

postgresql 中是否有任何选项?如果有的话请告诉我?

回答by Amrita

If I modify your

如果我修改你的

Select DATE_FORMAT(date_time, '%b %e, %Y, %T') from table_name

to

Select DATE_FORMAT(now(), '%b %e, %Y, %T')

it will return Aug 21, 2012, 16:51:30.

它会回来Aug 21, 2012, 16:51:30

You can do the same thing in Postgresql:

你可以在 Postgresql 中做同样的事情:

Select to_char(now(), 'Mon-dd-YYYY,HH24:MM:SS')

will return you Aug-21-2012,16:08:08

会回报你 Aug-21-2012,16:08:08

Hope your problem is sort out.

希望你的问题得到解决。

回答by Moyed Ansari

Try this

尝试这个

SELECT to_char(date_time, 'dd.mm.YYYY') from table_name

回答by a_horse_with_no_name

Use to_char():

使用 to_char():

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

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

I don't know what '%b %e, %Y, %T'produces as an output (and you didn't supply a sample output), so I cannot give you the equivalent format mask for Postgres.

我不知道'%b %e, %Y, %T'输出是什么(并且您没有提供示例输出),因此我无法为您提供 Postgres 的等效格式掩码。

The following produces an ANSI date:

以下生成 ANSI 日期:

select to_char(date_time, 'yyyy-mm-dd hh24:mi:ss')
from table_name;

回答by long

to_char, eg:

to_char,例如:

to_char(creation_date, 'FMDD.MM.YYYY, HH24:MI:SS') AS creation_date_f