Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1346482
  • 博文数量: 205
  • 博客积分: 6732
  • 博客等级: 准将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-04 17:59
文章分类

全部博文(205)

文章存档

2016年(1)

2015年(10)

2014年(1)

2013年(39)

2012年(23)

2011年(27)

2010年(21)

2009年(55)

2008年(28)

我的朋友

分类: Oracle

2010-01-19 18:13:13

oracle随机数 — dbms_random

ORACLE的PL/SQL提供了生成随机数和随机字符串的多种方式,罗列如下:

1、小数( 0 ~ 1)

     select dbms_random.value from dual

2、指定范围内的小数 ( 0 ~ 100 )
     select dbms_random.value(0,100) from dual


3、指定范围内的整数 ( 0 ~ 100 )

     select trunc(dbms_random.value(0,100)) from dual

4、长度为20的随机数字串

     select substr(cast(dbms_random.value as varchar2(38)),3,20) from dual

5、正态分布的随机数

     select dbms_random.normal from dual

6、随机字符串

     select dbms_random.string(opt, length) from dual

      opt可取值如下:
      'u','U'    :    大写字母
      'l','L'    :    小写字母
      'a','A'    :    大、小写字母
      'x','X'    :    数字、大写字母
      'p','P'    :    可打印字符

7、随机日期

     select to_date(2454084+TRUNC(DBMS_RANDOM.VALUE(0,365)),'J') from dual

     通过下面的语句获得指定日期的基数

     select to_char(sysdate,'J') from dual

8、生成GUID

     select sys_guid() from dual

--生成带分隔符(-)的GUID的自定义函数
create or replace function my_guid
return varchar2
is
    guid varchar(36);
    temp varchar(32);
begin
    temp:=sys_guid();
    guid:= substr(temp,1,8) || '-'
         ||substr(temp,9,4) || '-'
         ||substr(temp,13,4)|| '-'
         ||substr(temp,17,4)|| '-'
         ||substr(temp,21,12);
    return guid;
end;

阅读(947) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~