如何在 Oracle 中找到不同的黑白 TIMESTAMP 格式值?

2021-12-06 00:00:00 timestamp oracle

我有两个 timestamp 列:arrTimedepTime.我需要找到公共汽车晚点的数量.我尝试了以下方法:

I have two timestamp columns: arrTime and depTime. I need to find the number of munites the bus is late. I tried the following:

SELECT RouteDate, round((arrTime-depTime)*1440,2) time_difference
FROM ...

我收到以下错误:inconsistent datatype.预期的数字,但间隔天到秒

如何解析分钟数?

如果我只是减去:SELECT RouteDate, arrTime-depTime)*1440 time_difference结果正确但格式不正确:

If i simply subtract: SELECT RouteDate, arrTime-depTime)*1440 time_difference The result is correct but not well formatted:

 time_difference
  +00000000 00:01:00 0000000

推荐答案

时间戳算法的结果是 INTERVAL 数据类型.你有一个间隔天到第二个...

The result of timestamp arithmetic is an INTERVAL datatype. You have an INTERVAL DAY TO SECOND there...

如果您想要分钟数,一种方法是使用 EXTRACT(),例如:

If you want the number of minutes one way would be to use EXTRACT(), for instance:

select extract( minute from interval_difference ) * 60 
        + extract( hour from interval_difference ) * 60
        + extract( day from interval_difference ) * 60 * 24
  from ( select systimestamp - (systimestamp - 1) as interval_difference
           from dual )

或者,您可以使用带有日期的技巧:

Alternatively you can use a trick with dates:

select sysdate + (interval_difference * 1440) - sysdate
  from (select systimestamp - (systimestamp - 1) as interval_difference
          from dual )

技巧"版本的工作原理是运算符的优先顺序 以及日期和时间戳算法之间的差异.

The "trick" version works because of the operator order of precedence and the differences between date and timestamp arithmetic.

最初的操作是这样的:

date + ( interval * number ) - date

如文档中所述:

Oracle 先计算括号内的表达式,然后再计算括号外的表达式.

Oracle evaluates expressions inside parentheses before evaluating those outside.

因此,第一个操作是将间隔乘以 1,440.一个区间,即一个离散的时间段,乘以一个数就是另一个离散的时间段,请参阅有关日期时间和间隔算术的文档.所以,这个操作的结果是一个区间,给我们留下:

So, the first operation performed it to multiply the interval by 1,440. An interval, i.e. a discrete period of time, multiplied by a number is another discrete period of time, see the documentation on datetime and interval arithmetic. So, the result of this operation is an interval, leaving us with:

date + interval - date

此处加号运算符优先于减号.这样做的原因可能是间隔减去日期是无效操作,但文档也暗示了这种情况(没有出来说出来).因此,执行的第一个操作是日期 + 间隔.一个日期加上一个间隔就是一个日期.离开只是

The plus operator takes precedence over the minus here. The reason for this could be that an interval minus a date is an invalid operation, but the documentation also implies that this is the case (doesn't come out and say it). So, the first operation performed is date + interval. A date plus an interval is a date. Leaving just

date - date

根据文档,这会生成一个表示天数的整数.但是,您将原始间隔乘以 1,440,因此现在代表了 1,440 倍的天数.然后剩下的就是秒数.

As per the documentation, this results in an integer representing the number of days. However, you multiplied the original interval by 1,440, so this now represented 1,440 times the amount of days it otherwise would have. You're then left with the number of seconds.

值得注意的是:

当间隔计算返回日期时间值时,结果必须是实际的日期时间值,否则数据库返回错误.例如,接下来的两条语句返回错误:

When interval calculations return a datetime value, the result must be an actual datetime value or the database returns an error. For example, the next two statements return errors:

诡计"方法会失败,很少,但它仍然会失败.一如既往,最好正确地进行.

The "trick" method will fail, rarely but it will still fail. As ever it's best to do it properly.

相关文章