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

后端 未结 4 429
粉色の甜心
粉色の甜心 2021-01-04 01:34

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 NUMB         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-04 01:53

    I think you can adapt the function to substract the two timestamps:

    return  EXTRACT(MINUTE FROM 
      TO_TIMESTAMP(to_char(p_date1,'DD-MON-YYYY HH:MI:SS'),'DD-MON-YYYY HH24:MI:SS')
    -
      TO_TIMESTAMP(to_char(p_date2,'DD-MON-YYYY HH:MI:SS'),'DD-MON-YYYY HH24:MI:SS')
    );
    

    I think you could simplify it by just using CAST(p_date as TIMESTAMP).

    return  EXTRACT(MINUTE FROM cast(p_date1 as TIMESTAMP) - cast(p_date2 as TIMESTAMP));
    

    Remember dates and timestamps are big ugly numbers inside Oracle, not what we see in the screen; we don't need to tell him how to read them. Also remember timestamps can have a timezone defined; not in this case.

提交回复
热议问题