En supposant que vos dates ont toujours une composante horaire à minuit, alors :
SELECT *
FROM your_table
WHERE your_date_column IN (
TRUNC( SYSDATE ),
TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY,
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -24 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -36 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -48 )
);
(Vous devez ajouter 1 jour avant de tronquer pour correspondre à la casse lorsqu'aujourd'hui est le 31 décembre)
La solution simple serait d'utiliser TRUNC
à la date.
SELECT *
FROM your_table
WHERE TRUNC( your_date_column ) IN (
TRUNC( SYSDATE ),
TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY,
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -24 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -36 ),
ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -48 )
);
Mais alors vous ne pouvez pas utiliser un index sur la colonne et auriez besoin d'un index basé sur la fonction sur TRUNC( your_date_column )
pouvoir utiliser un index.
La solution la plus compliquée, qui peut utiliser des index, est :
SELECT *
FROM your_table
WHERE ( your_date_column >= TRUNC( sysdate )
AND your_date_column < TRUNC( sysdate ) + INTERVAL '1' DAY
)
OR ( your_date_column >= TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY
AND your_date_column < TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -12 )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -24 )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -36 )
)
OR ( your_date_column >= ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ) - INTERVAL '1' DAY, -12 )
AND your_date_column < ADD_MONTHS( TRUNC( SYSDATE + INTERVAL '1' DAY, 'YYYY' ), -48 )
);