You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Presto中如何将带小数秒的时间戳转换为秒级日期写入Oracle

问题说明

需要将Hive中存储为string类型、格式为2022-06-15 10:21:05.698000000的时间值,转换为精确到秒的2022-06-15 10:21:05格式,以插入到Oracle对应date类型的字段中。
当前使用的查询SQL:

select hive_date,cast(coalesce(substr(A.hive_date, 1,19),substr(A.hive_date2,1,19)) as timestamp) 
as oracle_date from test A limit 10;

当前返回结果中oracle_date字段携带.000的毫秒后缀,不符合插入要求。

解决方案

以下两种写法都可以直接得到精确到秒的时间值,完全适配Oracle date类型的精度要求:

  • 直接指定timestamp精度为0(适配现有写法,改动最小)
    你已经通过substr(xxx,1,19)拿到了精确到秒的时间字符串,只需要将cast的目标类型改为TIMESTAMP(0),就会自动截断小数秒部分,不会返回毫秒后缀:
SELECT 
  hive_date,
  CAST(substr(COALESCE(A.hive_date, A.hive_date2), 1, 19) AS TIMESTAMP(0)) AS oracle_date
FROM test A 
LIMIT 10;
  • 用时间截断函数处理(容错性更高)
    如果存在时间字符串长度不固定的场景,可以先将原始字符串转为时间类型,用date_trunc截断到秒级别后再输出,不需要依赖字符串截取的固定长度:
SELECT 
  hive_date,
  CAST(date_trunc('second', CAST(COALESCE(A.hive_date, A.hive_date2) AS TIMESTAMP)) AS TIMESTAMP(0)) AS oracle_date
FROM test A 
LIMIT 10;

注意:Oracle的date类型本身仅支持精确到秒,上述两种写法返回的结果精度完全匹配,直接通过导入工具写入即可,不需要额外做格式转换。

内容的提问来源于stack exchange,提问作者Sonu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 00:39:18