简体   繁体   中英

How to convert ORACLE date and timestamp types to string with timezone?

How to convert ORACLE date and timestamp types into a string with timezone formated like this 2020-12-31T21:00:00.000Z ?

Tried to search but unsuccessfully

Assuming the date or timestamp already represents a time in UTC and doesn't need to be converted from another time zone, you just need to_char :

select to_char(timestamp '2020-12-31 21:00:00', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z")
from dual
2020-12-31T21:00:00.000Z

The "T" and "Z" are character literals .

If it's a date the .000 part can be treated as a literal too, since dates don't have fractional seconds and FF isn't valid for a date:

select to_char(cast(timestamp '2020-12-31 21:00:00' as date), 'YYYY-MM-DD"T"HH24:MI:SS".000Z"')
from dual
2020-12-31T21:00:00.000Z

Or cast the date to a timestamp if you prefer.

If you do need to adjust the time zone then you can do that before converting to a string.

db<>fiddle

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