简体   繁体   English

如何将 ORACLE 日期和时间戳类型转换为带时区的字符串?

[英]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 ?如何将 ORACLE 日期和时间戳类型转换2020-12-31T21:00:00.000Z区格式为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 :假设日期或时间戳已经表示 UTC 时间并且不需要从另一个时区转换,您只需要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 . "T""Z"字符文字

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:如果它是日期,则.000部分也可以被视为文字,因为日期没有小数秒并且FF对日期无效:

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 数据库<>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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