Skip to content
Advertisement

How to calculate a time period in years and months

I want to be able to get the time a customer has been registered for, so far I’ve only got the months but I want to get the years and the months. There’s probably a function I’m not using that works out years and months rather than just months. Thanks

SELECT MONTHS_BETWEEN(TO_DATE(SYSDATE,'DD-MON-YYYY'), (TO_DATE(REGISTER_DATE,'DD-MON-YYYY'))) AS 
"Registered for"
FROM A3_CUSTOMER
WHERE CUSTOMER_ID = 'C99166'

Advertisement

Answer

You can use months_between as follows:

Trunc(MONTHS_BETWEEN(SYSDATE, REGISTER_DATE)/ 12) as year,
Trunc(Mod(MONTHS_BETWEEN(SYSDATE, REGISTER_DATE), 12))  as month
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement