简体   繁体   English

Null DB2 SQL 中的 TIMESTAMPDIFF 结果

[英]Null Results From TIMESTAMPDIFF in DB2 SQL

I am calculating a TIMESTAMPDIFF from timestamps that can have a fairly large range of time intervals between them, from a few tenths of a second to 60+mins.我正在根据时间戳计算 TIMESTAMPDIFF,它们之间的时间间隔范围相当大,从十分之几秒到 60 分钟以上。 Since the DB2 TIMESTAMPDIFF() function in DB2 returns an integer as a result, I am using microseconds as my numeric interval expression. Since the DB2 TIMESTAMPDIFF() function in DB2 returns an integer as a result, I am using microseconds as my numeric interval expression. TIMESTAMPDIFF DB2 documentation states: TIMESTAMPDIFF DB2 文档状态:

Microseconds (the absolute value of the duration must be less than 3547.483648)微秒(持续时间的绝对值必须小于 3547.483648)

This equates to approximately ~59 minutes - so any interval over this amount returns as a null value which is the issue I'm trying to address.这相当于大约 59 分钟 - 因此超过此数量的任何时间间隔都会返回为 null 值,这是我要解决的问题。

Sample queries/timestamps I'm working with in the data:我在数据中使用的示例查询/时间戳:

select timestampdiff(1, char(timestamp('2022-09-12 14:30:40.444896') - timestamp('2022-09-12 14:30:40.115789'))) from sysibm.SYSDUMMY1

select timestampdiff(1, char(timestamp('2022-09-12 15:59:14.548636') - timestamp('2022-09-12 14:56:10.791140'))) from sysibm.SYSDUMMY1

The second query above is an example that returns a null value as the result exceeds the maximum result interval limit.上面的第二个查询是一个示例,它在结果超过最大结果间隔限制时返回 null 值。 I am pigeon-holed into using microseconds as my interval as results less than 1 whole second are still valid.我习惯于使用微秒作为间隔,因为不到 1 秒的结果仍然有效。

Are there any methods of working around this limit to return results exceeding the limit?是否有任何方法可以解决此限制以返回超出限制的结果?

When you subtract dates or timestamps, you end up with a duration..当您减去日期或时间戳时,您最终会得到一个持续时间..

A duration is a number formatted as yyyymmddhhmmss.zzzzzzzzzzzz .持续时间是一个格式为yyyymmddhhmmss.zzzzzzzzzzzz的数字。
From the manual:从手册:

 A timestamp duration represents a number of years, months, days, hours, minutes, seconds, and fractional seconds, expressed as a DECIMAL(14+s,s) number, where s is the number of digits of fractional seconds ranging from 0 to 12. To be properly interpreted, the number must have the format yyyymmddhhmmss.zzzzzzzzzzzz, where yyyy, mm, dd, hh, mm, ss, and zzzzzzzzzzzz represent, respectively, the number of years, months, days, hours, minutes, seconds, and fractional seconds. The result of subtracting one timestamp value from another is a timestamp duration with scale that matches the maximum timestamp precision of the timestamp operands.
select timestamp('2022-09-12 15:59:14.548636') - timestamp('2022-09-12 14:56:10.791140') from sysibm.SYSDUMMY1;

returns 10303.757496返回10303.757496

And is read as 1 hour, 3 minutes, 3.757496 seconds读作 1 小时 3 分 3.757496 秒

So if you wanted to, you can do the math yourself.因此,如果您愿意,您可以自己计算。 Better yet build your own UDF that returns a big integer or even larger as a decimal value.更好的是构建您自己的 UDF,它返回一个大的 integer 甚至更大的十进制值。

SELECT
  A, B
,   TIMESTAMPDIFF (2, CHAR (A - B)) * 1000
  + (MICROSECOND (A) - MICROSECOND (B)) / 1000
  AS DIFF_MS
FROM 
(
  VALUES
  (timestamp('2022-09-12 14:30:40.444896'), timestamp('2022-09-12 14:30:40.115789'))
, (timestamp('2022-09-12 15:59:14.548636'), timestamp('2022-09-12 14:56:10.791140'))
) T (A, B)
A一个 B DIFF_MS DIFF_MS
2022-09-12 14:30:40.444896 2022-09-12 14:30:40.444896 2022-09-12 14:30:40.115789 2022-09-12 14:30:40.115789 329 329
2022-09-12 15:59:14.548636 2022-09-12 15:59:14.548636 2022-09-12 14:56:10.791140 2022-09-12 14:56:10.791140 3782758 3782758

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

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