postgresql 如何更改 postgres 中的日期格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42235129/
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-10-21 02:31:23  来源:igfitidea点击:

How to change a date format in postgres?

postgresql

提问by user2926497

I am trying to convert a character varying date field to the format YYYYMMDD and below is the select query I tried. The output format is 1999-04-27 (YYYY-MM-DD). Admindate is the field name in the table.

我正在尝试将字符可变日期字段转换为 YYYYMMDD 格式,下面是我尝试的选择查询。输出格式为 1999-04-27 (YYYY-MM-DD)。Admindate 是表中的字段名称。

select to_date( admindate,'MMDDYYYY')  from test;

Can someone please advice what I am doing wrong?

有人可以建议我做错了什么吗?

回答by Gurwinder Singh

Use to_charto convert the converted date into the desired format.

使用to_char该转换日期转换成所需的格式。

select to_char(to_date(admindate, 'MMDDYYYY'), 'YYYYMMDD')
from test;