I ran into the challenge of updating a database (oracle) table with a random date and thought that my solution might help someone else with a similar challenge. Here goes…
First you need to get the Julian day number for your start date. Start date is the date from where you want to begin the randomization. Use the following SQL statement to get that number…
SELECT TO_CHAR(TO_DATE('2010-01-01', 'YYYY-MM-DD'), 'J') FROM DUAL;
When you run the above SQL query, the cursor should show ‘2455198‘. Now that you have the julian value for your start date, you can use the following statement to generate a random value between your start date and any number of additional dates. In the example below, I am adding 120 additional days to my start date so my range will be Jan-01-2010 till Apr-30-2010.
SELECT TO_DATE(TRUNC(DBMS_RANDOM.VALUE(2455198, 2455198+120)), 'J') FROM DUAL;
From here, a simple update statement should do the trick!
UPDATE table_name SET column_name = (SELECT TO_DATE(TRUNC(DBMS_RANDOM.VALUE(2455198, 2455198+120)), 'J') FROM DUAL);
Hope this saved you some time!
No related posts...sorry
hook me up!