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
How to change a date format in postgres?
提问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_char
to convert the converted date into the desired format.
使用to_char
该转换日期转换成所需的格式。
select to_char(to_date(admindate, 'MMDDYYYY'), 'YYYYMMDD')
from test;