Oracle 没有datetime 数据类型。它有date,它有一天和一秒的时间。它有一个timestamp,它也有一天和秒的时间,可选的小数秒和时区。将date 转换为timestamp 只会添加始终为0 的小数秒。date 和timestamp 数据类型都没有格式。 varchar2 将具有格式。如果列是 date 数据类型,则您的代码在语法上是有效的。我不确定你得到的结果与你想要的结果有什么不同,因为你没有向我们展示你的样本数据或预期的结果,当你说某些东西没有被转换时,你也没有告诉我们你的意思。
假设这两列实际上是date 类型,您的代码似乎没问题,您只想使用greatest 函数来获取最新日期。见this fiddle
with cte as (
select sysdate dat_cr, sysdate + 1 dat_mod
from dual
)
select cast(dat_cr as timestamp) ts_cr,
cast(dat_mod as timestamp) ts_mod,
cast( greatest( dat_cr, dat_mod ) as timestamp ) ts_greatest
from cte;
TS_CR TS_MOD TS_GREATEST
02-SEP-21 08.25.38.000000 AM 03-SEP-21 08.25.38.000000 AM 03-SEP-21 08.25.38.000000 AM
请注意,将三个时间戳转换为要显示给人类的字符串是由会话的nls_timestamp_format 控制的。
如果您想通过返回 not null 中的任何一个日期来处理 null 日期,您可以使用 coalesce 和 case 语句
with cte as (
select sysdate dat_cr, sysdate + 1 dat_mod
from dual
union all
select null, sysdate from dual
union all
select sysdate, null from dual
)
select cast(dat_cr as timestamp) ts_cr,
cast(dat_mod as timestamp) ts_mod,
cast( case when dat_cr is null or dat_mod is null
then coalesce( dat_mod, dat_cr )
else greatest( dat_cr, dat_mod )
end
as timestamp ) ts_greatest
from cte;
见this fiddle