我有一个脚本,我可以运行来掩盖日期就好了。我的要求是日期不能向前移动,日历年不能改变,01-jan-xx不能改变。
我当前的代码会在-1到-85天之间随机更改日期;但是我不确定是否应该更改以查询Julian日期,以便我不会更改日历年...打开任何见解或帮助。
create or replace FUNCTION a_function (a_date DATE)
RETURN DATE
AS
a_val NUMBER;
a_val1 NUMBER;
a_date DATE;
a_date1 DATE;
BEGIN
a_val := DBMS_RANDOM.VALUE (1, 85);
a_date := a_date + a_val;
IF TO_CHAR (a_date, 'YYYY') <> TO_CHAR (a_date, 'YYYY')
THEN
a_val1 := DBMS_RANDOM.VALUE (-1, -85);
a_date1 := a_date + (a_val1);
RETURN a_date1;
ELSE
RETURN a_date;
END IF;
END;
答案 0 :(得分:2)
我会尝试像
这样的东西a_date
这将获取a_date
的一年中的一天,减去1,然后生成介于0和l_max_days之间的天数。因此,如果- 1
是1月10日,则l_max_days为9,我们将在0到8天之间添加到1年的第一天,给我们1月1日到1月9日之间的日期。您可以删除{{1}如果要允许返回的日期与输入日期匹配,请从l_max_days
开始。
答案 1 :(得分:1)
两个选项:首先,获得一个随机的早期日期/时间;第二,获得一个随机的早期日期(没有时间成分)。
Oracle 11g R2架构设置:
CREATE FUNCTION get_Random_Earlier_Datetime(
in_date DATE
) RETURN DATE
AS
BEGIN
RETURN TRUNC( in_date, 'Y' ) + DBMS_RANDOM.VALUE( 0, in_date - TRUNC( in_date, 'Y' ) );
END;
/
CREATE FUNCTION get_Random_Earlier_Day(
in_date DATE
) RETURN DATE
AS
BEGIN
RETURN TRUNC( in_date, 'Y' ) + FLOOR( DBMS_RANDOM.VALUE( 0, in_date - TRUNC( in_date, 'Y' ) ) );
END;
/
查询1 :
SELECT DATE '2015-01-01' + LEVEL - 1 AS "Date",
get_Random_Earlier_Datetime( DATE '2015-01-01' + LEVEL - 1 ),
get_Random_Earlier_Day( DATE '2015-01-01' + LEVEL - 1 )
FROM DUAL
CONNECT BY LEVEL <= 10
<强> Results 强>:
| Date | GET_RANDOM_EARLIER_DATETIME(DATE'2015-01-01'+LEVEL-1) | GET_RANDOM_EARLIER_DAY(DATE'2015-01-01'+LEVEL-1) |
|---------------------------|-------------------------------------------------------|--------------------------------------------------|
| January, 01 2015 00:00:00 | January, 01 2015 00:00:00 | January, 01 2015 00:00:00 |
| January, 02 2015 00:00:00 | January, 01 2015 14:11:37 | January, 01 2015 00:00:00 |
| January, 03 2015 00:00:00 | January, 01 2015 09:24:25 | January, 01 2015 00:00:00 |
| January, 04 2015 00:00:00 | January, 01 2015 00:45:22 | January, 03 2015 00:00:00 |
| January, 05 2015 00:00:00 | January, 03 2015 11:54:39 | January, 04 2015 00:00:00 |
| January, 06 2015 00:00:00 | January, 05 2015 03:55:55 | January, 01 2015 00:00:00 |
| January, 07 2015 00:00:00 | January, 04 2015 01:12:23 | January, 02 2015 00:00:00 |
| January, 08 2015 00:00:00 | January, 06 2015 22:10:37 | January, 02 2015 00:00:00 |
| January, 09 2015 00:00:00 | January, 06 2015 10:47:04 | January, 04 2015 00:00:00 |
| January, 10 2015 00:00:00 | January, 06 2015 07:20:25 | January, 08 2015 00:00:00 |