Oracle中产生各种随机数的方法

使用dbms_random包中的函数生成随机数

注:dbms_random.value(10,100)为左闭右开区间;

--生成一个0~1之间的随机小数 select dbms_random.value as random_number from dual; --生成一个0到100之间的整数随机数 select floor(dbms_random.value(0, 101)) as random_number from dual; --生成一个由10个字符组成的随机字符串,字符集为大写字母和数字 select dbms_random.string('U', 10) as random_string from dual; --随机打印5个(大小写)字母/字符/数字 select dbms_random.value(100, 0) a, dbms_random.string('u', 5) b, --随机打印5个大写字母 dbms_random.string('l', 5) c, --随机打印5个小写字母 dbms_random.string('a', 5) d, --随机打印5个字母 dbms_random.string('x', 5) e, --随机打印5个大写字母和数字 dbms_random.string('p', 5) f --随机打印5个可打印字符 from dual; --生成一个1~100的随机数(带小数位) select dbms_random.value(1, 100) from dual;

--根据年龄计算出生年份
select to_char(add_months(sysdate, -12 * 26), 'yyyy') as csny from dual;

--oracle生成两位随机的月份
select to_char(floor(dbms_random.value(1, 12)), 'FM00') as random_month from dual;
--说明:使用DBMS_RANDOM包来生成两位随机数,FM00格式会确保即使生成的数字为个位数,也会在前面补上0以确保结果是两位数

--根据年龄生成测试数据身份证号的方式
select age,
   '370300' || to_char(add_months(sysdate, -12 * age), 'yyyy') ||
to_char(floor(dbms_random.value(1, 12)), 'FM00') ||
floor(dbms_random.value(10, 31)) ||
round(dbms_random.value(1000, 9999), 0)
from table_name t;

原文链接:https://blog.csdn.net/m0_71406734/article/details/130514098

posted @ 2024-04-15 19:12  DAYTOY-105  阅读(87)  评论(0编辑  收藏  举报