您可以回复 Oracle 通过使用 YY 或 RR format model elements 转换来推断年份,或者连接世纪值并使用 YYYY。
如果您确实确定日期都是 21 世纪,那么使用连接和 YYYY:
to_date('20' || tistamp, 'yyyymmddhh24miss')
与使用 YY 的行为相同(使用当前日期来决定世纪):
to_date(tistamp, 'yymmddhh24miss')
如果所有年份都低于 50,那么 RR (which uses the current date's century or the last century depending on the supplied 2-digit year) 也会得到相同的结果:
to_date(tistamp, 'rrmmddhh24miss')
但是,如果任何值是 50 或以上,RR 和 YY/YYYY 的行为会有所不同。由于这些似乎是事件时间戳,因此它们不太可能在未来出现,但差异可能在某一天仍然很重要。 (但是,最终,假设 21 世纪也可能无效......)
快速演示差异,使用您的样本值和其他几个值,通过 CTE 提供:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
with your_table(tistamp) as (
select '16101211213' from dual
union all select '491231235959' from dual
union all select '500101000000' from dual
)
select to_date('20' || tistamp, 'yyyymmddhh24miss') as yyyy,
to_date(tistamp, 'yymmddhh24miss') as yy,
to_date(tistamp, 'rrmmddhh24miss') as rr
from your_table;
YYYY YY RR
------------------- ------------------- -------------------
2016-10-12 11:21:03 2016-10-12 11:21:03 2016-10-12 11:21:03
2049-12-31 23:59:59 2049-12-31 23:59:59 2049-12-31 23:59:59
2050-01-01 00:00:00 2050-01-01 00:00:00 1950-01-01 00:00:00
当然,所有这些也适用于to_timestamp();因为您没有小数秒或使用日期的时区信息应该没问题,只要您的客户知道 Oracle 日期有时间组件。