When writing code that deals with time, it would be very useful to be able to "set" the starting point for SYSDATE within a session.
For example, suppose I want to select one set of data if the query runs in March and a different set if it runs in April (perhaps
because the start of a new fiscal year is April 1). I'd like to be able to use SYSDATE in the query for clarity, but that means
I can't actually test the code behavior for April until April 1.
Currently, I just develop and test the code with a custom MY_SYSDATE function that returns a testing date value, but that requires
me to remember to revert the code to use the real SYSDATE (and to change/compile the custom function to use a new date).
What I'd really like to do initialize SYSDATE at the beginning of my session and have Oracle handle the differences for me through
the session, something like this:
SQL>alter session set date_baseline = to_date( '2017-04-01 04:30:12', 'yyyy-mm-dd hh24:mi:ss' ) ;
Then, all subsequent references to SYSDATE would be relative to 2017.04.01 00:00:00 and remain so until I either terminate my
session or run another alter command. Of course, we'd need to have this ability granted to us in DEV/TEST but not granted in
production.
Is this possible? If so, how do I do it?
Thanks!
Not for an individual session, but you can set the date for a whole database!
Setting fixed_date locks the database to a particular point in time:
select sysdate from dual;
SYSDATE
16-MAR-2017 10:23:18
alter system set fixed_date = '2017-01-01';
select sysdate from dual;
SYSDATE
01-JAN-2017 00:00:00
alter system set fixed_date = '2017-04-01 12:34:56';
select sysdate from dual;
SYSDATE
01-APR-2017 12:34:56
alter system set fixed_date = 'NONE';
select sysdate from dual;
SYSDATE
16-MAR-2017 10:23:21
Just make sure you set it back to 'NONE' once you're finished!