db2 从Oracle中的时间戳截断毫秒

dluptydi  于 2022-11-07  发布在  DB2
关注(0)|答案(3)|浏览(271)

我尝试使用TRUNC函数从Timestamp字段中截断毫秒数。在我的表中,timestamp是这样的:
2022年6月1日14时22分:37.965千
我想去掉毫秒。
我尝试的是以下查询:

SELECT * FROM SCHEMA.TABLE1 PT INNER JOIN SCHEMA.TABLE2 ST
                        ON (ST.PROPERTY1 = PT.PROPERTY1 AND
                        ST.PROPERTY2 = PT.PROPERTY2 AND
                        ST.PROPERTY3 = PT.PROPERTY3 AND
                        ST.PROPERTY4 = PT.PROPERTY4 AND
                        ST.PROPERTY5 = PT.PROPERTY5 AND
                        ST.PROPERTY6 = PT.PROPERTY6  AND
                        ST.PROPERTY7 = PT.PROPERTY7 AND
                        ST.PROPERTY8 = PT.PROPERTY8)
                        WHERE ST.ID = 4 AND TRUNC(TO_DATE(ST.TIMESTAMP), SECOND) = '2022-06-01-14:22:37'

我做错了什么?

vwoqyblh

vwoqyblh1#

如果要将TIMESTAMP四舍五入到最接近的秒数,请用途:

CAST(ts AS TIMESTAMP(0))

如果要将TIMESTAMP截断到当前秒的开头,请用途:

CAST(
  TRUNC(ts, 'MI') + INTERVAL '1' SECOND * TRUNC(EXTRACT(SECOND FROM ts))
  AS TIMESTAMP(0)
)

或者将其转换为日期,然后再转换回时间戳:

CAST(CAST(ts AS DATE) AS TIMESTAMP(0))

(with无论您需要什么精度)
例如:

SELECT ts,
       CAST(ts AS TIMESTAMP(0)) AS ts_rounded,
       CAST(
         TRUNC(ts, 'MI') + INTERVAL '1' SECOND * TRUNC(EXTRACT(SECOND FROM ts))
         AS TIMESTAMP(0)
       ) AS ts_trunc
FROM   (
  SELECT TIMESTAMP '2022-06-01 14:22:37.965000' AS ts
  FROM   DUAL
)

输出:
| 技术支持|TS_舍入|TS_截断|
| - -|- -|- -|
| 2022年6月1日14时22分|2022年6月1日14时22分38秒。|2022年6月1日14时22分37秒。|

  • db〈〉小提琴

但是,使用范围可能更简单:

SELECT *
FROM   SCHEMA.TABLE1 PT
       INNER JOIN SCHEMA.TABLE2 ST
       ON (ST.PROPERTY1 = PT.PROPERTY1 AND
           ST.PROPERTY2 = PT.PROPERTY2 AND
           ST.PROPERTY3 = PT.PROPERTY3 AND
           ST.PROPERTY4 = PT.PROPERTY4 AND
           ST.PROPERTY5 = PT.PROPERTY5 AND
           ST.PROPERTY6 = PT.PROPERTY6  AND
           ST.PROPERTY7 = PT.PROPERTY7 AND
           ST.PROPERTY8 = PT.PROPERTY8)
WHERE  ST.ID = 4
AND    st.timestamp >= TIMESTAMP '2022-06-01 14:22:37'
AND    st.timestamp <  TIMESTAMP '2022-06-01 14:22:37' + INTERVAL '1' SECOND;

(它将在st.timestamp列上使用索引)
或将值转换为字符串:

SELECT *
FROM   SCHEMA.TABLE1 PT
       INNER JOIN SCHEMA.TABLE2 ST
       ON (ST.PROPERTY1 = PT.PROPERTY1 AND
           ST.PROPERTY2 = PT.PROPERTY2 AND
           ST.PROPERTY3 = PT.PROPERTY3 AND
           ST.PROPERTY4 = PT.PROPERTY4 AND
           ST.PROPERTY5 = PT.PROPERTY5 AND
           ST.PROPERTY6 = PT.PROPERTY6  AND
           ST.PROPERTY7 = PT.PROPERTY7 AND
           ST.PROPERTY8 = PT.PROPERTY8)
WHERE  ST.ID = 4
AND    TO_CHAR(st.timestamp, 'YYYY-MM-DD HH24:MI:SS') = '2022-06-01 14:22:37';

(它不会使用st.timestamp的索引,而是需要TO_CHAR(st.timestamp, 'YYYY-MM-DD HH24:MI:SS')上的单独的基于函数的索引。)

7gyucuyw

7gyucuyw2#

我只使用具有适当格式的TO_CHAR来获得您想要的结果:

WHERE TO_CHAR(ST.TIMESTAMP, 'YYYY-MM-DD-HH24:MI:SS') = '2022-06-01-14:22:37'
h22fl7wq

h22fl7wq3#

另一种截断到秒精度的方法是转换为日期:

select cast(timestamp '2022-06-01 14:22:37.965' as date) from dual

2022-06-01 14:22:37

然后将其与另一个日期进行比较,或者转换回一个时间戳与另一个时间戳进行比较。(无论哪种方式,都不要与字符串进行比较--只有在将时间戳值也转换为字符串时才这样做,而且要显式地使用相同的格式。不要对日期或时间戳值使用to_date()--您依赖于隐式转换和NLS设置。)
db<>fiddle

相关问题