Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: update a date field with fixed date but random hours/minutes
Mark C. Stock wrote:
> > "Harry331"
> > | But I'm not sure how to come up with the final SQL to update Exam_Date
> > | so that each record will have a different (and random) hh24:mi value.
> > |
> > | Any help appreciated.
> > |
[snip]
> convert the sysdate-1 date (not time) values with to_char, append the
> results or your random hours and minutes, then convert the resulting
> character expression back to a date with to_date
>
> presumably dbms_random will be invoked twice for each row so you will get
> random hours and random minutes. if for some reason that doesn't work,
> repost, you might need to update some sort of a correlated subquery
Thanks for the hint.
Below is what I've tried so far.
But my solution (4th SQL) ran forever. What's wrong with it?
1st SQL (select the Study_ID candudates)
Study_ID
1000001
1000011
1000012
2nd SQL (generate a date string)
2 ||' ' 3 ||(select trunc(dbms_random.value(0,24)) from dual) 4 ||':' 5 ||(select trunc(dbms_random.value(0,60)) from dual) 6 from dual;
2006-03-02 16:43
3rd SQL (convert the date string to_date)
3 ||' ' 4 ||(select trunc(dbms_random.value(0,24)) from dual) 5 ||':' 6 ||(select trunc(dbms_random.value(0,60)) from dual) 7 from dual), 8 'YYYY-MM-DD hh24:mi')
03-02-2006
4th SQL (update exam_date to be yesterday's date with random hr/mi)
||' ' ||(select trunc(dbms_random.value(0,24)) from dual) ||':' ||(select trunc(dbms_random.value(0,60)) from dual) from dual), 'YYYY-MM-DD hh24:mi')