ORACLE get the random time function of the specified date

 

CREATE OR REPLACE FUNCTION "DW_EVENTCENTER"."R_TIME" 
(V_DATE IN VARCHAR2, V_BEGIN IN NUMBER := 0, V_END IN NUMBER := 1) RETURN DATE
IS
  R_DATE DATE ;
BEGIN
  SELECT TO_DATE(V_DATE,'YYYY-MM-DD') + DBMS_RANDOM.VALUE(V_BEGIN ,V_END) INTO R_DATE FROM DUAL;
  RETURN R_DATE ;
END R_TIME;

When calling, you can call the function through the select query

SELECT R_TIME(20190101,0,1) FROM YXLS_LEFT_03  

YXLS_LEFT_03 How many pieces of data will appear in this table at random times  

The above V_DATE V_BEGIN V_END are the three parameters passed in when calling the function, and the following 0 and 1 are the 0 to 24 o’clock of the day passed in.

So if you want to take the 12 o'clock to 24 o'clock on January 1, 2019, you can set V_BEGIN to 0.5    

After that, the whole function can return the random time of a certain day you want. Of course, you also want to get a certain period of time, that is, not just a random time of one day. You can call the following SELECT statement through the function.

SELECT to_date(TRUNC(DBMS_RANDOM.VALUE(
       to_number(to_char(to_date('20190101','yyyymmdd'),'J')),
       to_number(to_char(to_date('20190501','yyyymmdd')+1,'J')))),'J')+
       DBMS_RANDOM.VALUE(1,3600)/3600
       prize_time
FROM dual;
 

This function call method is not written, if you are interested, you can try it yourself.

 

Guess you like

Origin blog.csdn.net/qq_37823979/article/details/97931131