如何使用 SQL 将日期格式转换为 YYYY-MM-DD

How do I convert date format to YYYY-MM-DD with SQL

我有一个 api return 一个 date/time 的 UTC,我希望我的数据库查询匹配这种格式。我要求的日期格式是:

2015-04-16T13:41:00.000+0000

到目前为止,我的查询如下所示:

SUBSTR(To_Char(cast(booking_date as timestamp) at time zone 'UTC'), 0, 24) AS bd

哪个 returns:

16-APR-15 13.41.00.00000

Oracle SQL 开发人员

中的当前查询是 运行

如图 in the documentation 所示,您可以通过双引号将文本文字包含在日期格式中,其余日期格式元素相当标准:

to_char(cast(booking_date as timestamp) at time zone 'UTC',
  'yyyy-mm-dd"T"hh24:mi:ss.ff3"+0000"')

快速演示:

select to_char(cast(sysdate as timestamp) at time zone 'UTC',
  'yyyy-mm-dd"T"hh24:mi:ss.ff3"+0000"') as utc_timestamp
from dual;

UTC_TIMESTAMP                    
----------------------------------
2015-04-16T16:07:53.000+0000