Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generating TIMESTAMPs without fraction

PugzlyDec 6 2022

I have a function which generates random TIMESTAMPs between date ranges that is working as expected 

Is there a way to produce the output with the fractional values of all zeros? I would prefer modifying this function by keeping the DEFAULT with the current functionality. If that can't be done I would be amenable to creating a new function.

Thanks in advance to all who answer and your time and expertise.

ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'DD-MON-YYYY  HH24:MI:SS.FF';

CREATE OR REPLACE FUNCTION random_timestamp(
      p_from IN TIMESTAMP,
      p_to   IN TIMESTAMP
    ) RETURN TIMESTAMP
   IS
 BEGIN
      RETURN p_from + DBMS_RANDOM.VALUE() * (p_to - p_from + interval '1' day);
 END random_timestamp;
/

SELECT
        random_timestamp(TIMESTAMP '2022-12-01 00:00:00', TIMESTAMP '2022-12-30 00:00:00') as ts
    FROM dual CONNECT BY level <= 10

TS
25-DEC-2022  08:28:52.513012
04-DEC-2022  03:51:02.863990
04-DEC-2022  04:30:08.122146
26-DEC-2022  17:04:27.572859
07-DEC-2022  00:56:40.307947
07-DEC-2022  16:31:29.670476
12-DEC-2022  05:43:28.033367
10-DEC-2022  04:49:30.403739
27-DEC-2022  03:29:20.624047
13-DEC-2022  02:34:07.480636

This post has been answered by Frank Kulash on Dec 6 2022
Jump to Answer
Comments
Post Details
Added on Dec 6 2022
12 comments
722 views