tags:

views:

95

answers:

2

What is the best way to randomize the time part for a DATE column, using Oracle 10g?

For example, the date portion for the data was set as follows:

UPDATE table_name SET column_ts = SYSDATE - 120 + MOD(ROWNUM, 35)

I would like the time portion to have a different value for each row.

+3  A: 

Choose a random number between 0 and 86400 (number of seconds in a day)

Add random / 86400 to your date.

SELECT TRUNC(SYSDATE)+DBMS_RANDOM.value(0, 86400-1)/86400 FROM DUAL

ADDITION:

UPDATE table_name
SET    column_ts = SYSDATE - 120 + MOD(ROWNUM, 35) + DBMS_RANDOM.value(0, 86400-1)/86400;
cagcowboy
UPDATE table_nameSET column_ts = SYSDATE - 120 + MOD(ROWNUM, 35) + DBMS_RANDOM.value(0, 86400-1)/86400;
cagcowboy
Perfect. Thank you!
Dave Jarvis
+2  A: 
select trunc(sysdate)+dbms_random.value from dual;
Chi
If DBMS_RANDOM.VALUE returns more than 86400, this will potentially increase the day. This is probably unwanted.
cagcowboy
DBMS_RANDOM.VALUE is defined to return a number between 0 and 1
Chi
(if you don't pass in any arguments)
Chi
Can it return 1.0?
jva