views:

45

answers:

2

Certain event needs to happen before the end of the year when it's 10 years since the previous event. I'm issuing a query like this to calculate the deadline:

SELECT
    :previous_date AS previous_date,
    ADD_MONTHS(
        TO_DATE(
            EXTRACT(
                YEAR FROM TO_DATE(:previous_date, 'YYYY-MM-DD HH24-MI-SS')
            ) || '-12-31 23-59-59', 'YYYY-MM-DD HH24-MI-SS'
        ), 12*10
    ) AS deadline
FROM DUAL

E.g.:

PREVIOUS_DATE DEADLINE                  
------------- ------------------------- 
2008-07-15    31/12/2018 23:59:59   

I works fine so far but... Am I missing some function that can make the query more readable?

+3  A: 

You could use the trunc so that you get

SELECT add_months(trunc(sysdate, 'YYYY'), 12*11) - (1/86400) from dual

The (1/1440) takes a second off the 11 years, to give you 23:59:59

DJIDave
Looks good. Though I guess you'd do `1/86400`, wouldn't you?
Álvaro G. Vicario
Yep sorry, 1440 is minutes, and 86400 is seconds
DJIDave
A: 

That's not really an answer but if the previous_date parameter was a date and not a string and you treated your deadline as exclusive (date < deadline rather than date <= deadline), your select would look like:

SELECT 
    :previous_date AS previous_date, 
    ADD_MONTHS( previous_date, 10 * 12) AS deadline 
FROM DUAL 
vc 74
I'm not sure I get your point. I need to store the actual deadline in a DATE column: the comparison is not done on-the-fly. BTW, there's no `ADD_YEARS()` function in Oracle.
Álvaro G. Vicario
Sorry, I looked on the internet and thought such a function existed (http://download.oracle.com/docs/cd/A60725_05/html/comnls/us/per/ffugax15.htm). What I mean is you could store the exclusive date, i.e. previous_date + 10 years, and when you perform the comparison consider it as an exclusive date.
vc 74