简体   繁体   中英

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

I have an api return a date/time in UTC and want my database query to match this format. The date format I require is:

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

So far my query looks like this:

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

Which returns:

16-APR-15 13.41.00.00000

The query is currently run in Oracle SQL Developer

As shown in the documentation , you can include text literals in the date format by double-quoting them, and the rest of the date format elements are fairly standard:

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

Quick demo:

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     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM