Oracle:如何减去两个日期并获得结果的分钟数

2021-12-24 00:00:00 date oracle plsql

我编写了这个函数来获取某个日期的分钟数,但我无法获取两个日期之间的分钟数,如何获取?

I wrote this function to get minutes from a date, but I cannot get minutes between two dates, How to get that ?

FUNCTION get_minute(p_date DATE)
RETURN NUMBER
IS
BEGIN
    IF p_date IS NOT NULL THEN
        return  EXTRACT(MINUTE FROM TO_TIMESTAMP(to_char(p_date,'DD-MON-YYYY HH:MI:SS'),'DD-MON-YYYY HH24:MI:SS'));
    ELSE
        RETURN 0;
    END IF;
END get_minute;

推荐答案

当你在 Oracle 中减去两个日期时,你会得到两个值之间的天数.所以你只需要在几分钟内乘以得到结果:

When you subtract two dates in Oracle, you get the number of days between the two values. So you just have to multiply to get the result in minutes instead:

SELECT (date2 - date1) * 24 * 60 AS minutesBetween
FROM ...

相关文章