Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle : how to subtract two dates and get minutes of the result

Tags:

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;
like image 214
Frank Avatar asked Nov 16 '12 00:11

Frank


People also ask

How do I find the difference between two dates and time in Oracle?

To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND . The first number you see is the number of whole days that passed from departure to arrival .

Can we subtract two dates in Oracle?

Answer: Oracle supports date arithmetic and you can make expressions like "date1 - date2" using date subtraction to get the difference between the two dates. Once you have the date difference, you can use simple techniques to express the difference in days, hours, minutes or seconds.

How do I subtract dates in Oracle?

Use the @DATEDIFF function to calculate the difference between two dates or datetimes, in days or seconds. The difference between the specified dates. Valid values can be: DD , which computes the difference in days.


1 Answers

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 ...
like image 135
lc. Avatar answered Oct 25 '22 08:10

lc.