110个oracle常用函数总结
110个oracle常用函数总结
1. ascii
返回与指定的字符对应的十进制数;
sql> select ascii(a) a,ascii(a) a,ascii(0) zero,ascii( ) space from dual;
a a zero space
--------- --------- --------- ---------
65 97 48 32
2. chr
给出整数,返回对应的字符;
sql> select chr(54740) zhao,chr(65) chr65 from dual;
zh c
-- -
赵 a
3. concat
连接两个字符串;
sql> select concat(010-,88888888)||转23 高乾竞电话 from dual;
高乾竞电话
----------------
010-88888888转23
4. initcap
返回字符串并将字符串的第一个字母变为大写;
sql> select initcap(smith) upp from dual;
upp
-----
smith
5.instr(c1,c2,i,j)
在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
c1 被搜索的字符串
c2 希望搜索的字符串
i 搜索的开始位置,默认为1
j 出现的位置,默认为1
sql> select instr(oracle traning,ra,1,2) instring from dual;
instring
---------
9
6.length
返回字符串的长度;
sql> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from gao.nchar_tst;
name length(name) addr length(addr) sal length(to_char(sal))
------ ------------ ---------------- ------------ --------- --------------------
高乾竞 3 北京市海锭区 6 9999.99 7
7.lower
返回字符串,并将所有的字符小写
sql> select lower(aabbccdd)aabbccdd from dual;
aabbccdd
--------
aabbccdd
8.upper
返回字符串,并将所有的字符大写
sql> select upper(aabbccdd) upper from dual;
upper
--------
aabbccdd
9.rpad和lpad(粘贴字符)
rpad 在列的右边粘贴字符
lpad 在列的左边粘贴字符
sql> select lpad(rpad(gao,10,*),17,*)from dual;
lpad(rpad(gao,1
-----------------
*******gao*******
不够字符则用*来填满
10.ltrim和rtrim
ltrim 删除左边出现的字符串
rtrim 删除右边出现的字符串
sql> select ltrim(rtrim( gao qian jing , ), ) from dual;
ltrim(rtrim(
-------------
gao qian jing
11.substr(string,start,count)
取子字符串,从start开始,取count个
sql> select substr(13088888888,3,8) from dual;
substr(
--------
08888888
12.replace(string,s1,s2)
string 希望被替换的字符或变量
s1 被替换的字符串
s2 要替换的字符串
sql> select replace(he love you,he,i) from dual;
replace(h
----------
i love you
13.soundex
返回一个与给定的字符串读音相同的字符串
sql> create table table1(xm varchar(8));
sql> insert into table1 values(weather);
sql> insert into table1 values(wether);
sql> insert into table1 values(gao);
sql> select xm from table1 where soundex(xm)=soundex(weather);
xm
--------
weather
wether
14.trim(s from string)
leading 剪掉前面的字符
trailing 剪掉后面的字符
如果不指定,默认为空格符
15.abs
返回指定值的绝对值
sql> select abs(100),abs(-100) from dual;
abs(100) abs(-100)
--------- ---------
100 100
16.acos
给出反余弦的值
sql> select acos(-1) from dual;
acos(-1)
---------
3.1415927
17.asin
给出反正弦的值
sql> select asin(0.5) from dual;
asin(0.5)
---------
.52359878
18.atan
返回一个数字的反正切值
sql> select atan(1) from dual;
atan(1)
---------
.78539816
19.ceil
返回大于或等于给出数字的最小整数
sql> select ceil(3.1415927) from dual;
ceil(3.1415927)
---------------
4
20.cos
返回一个给定数字的余弦
sql> select cos(-3.1415927) from dual;
cos(-3.1415927)
---------------
-1
21.cosh
返回一个数字反余弦值
sql> select cosh(20) from dual;
cosh(20)
---------
242582598
22.exp
返回一个数字e的n次方根
sql> select exp(2),exp(1) from dual;
exp(2) exp(1)
--------- ---------
7.3890561 2.7182818
23.floor
对给定的数字取整数
sql> select floor(2345.67) from dual;
floor(2345.67)
--------------
2345
24.ln
返回一个数字的对数值
sql> select ln(1),ln(2),ln(2.7182818) from dual;
ln(1) ln(2) ln(2.7182818)
--------- --------- -------------
0 .69314718 .99999999
25.log(n1,n2)
返回一个以n1为底n2的对数
sql> select log(2,1),log(2,4) from dual;
log(2,1) log(2,4)
--------- ---------
0 2
26.mod(n1,n2)
返回一个n1除以n2的余数
sql> select mod(10,3),mod(3,3),mod(2,3) from dual;
mod(10,3) mod(3,3) mod(2,3)
--------- --------- ---------
1 0 2
27.power
返回n1的n2次方根
sql> select power(2,10),power(3,3) from dual;
power(2,10) power(3,3)
----------- ----------
1024 27
28.round和trunc
按照指定的精度进行舍入
sql> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
round(55.5) round(-55.4) trunc(55.5) trunc(-55.5)
----------- ------------ ----------- ------------
56 -55 55 -55
29.sign
取数字n的符号,大于0返回1,小于0返回-1,等于0返回0
sql> select sign(123),sign(-100),sign(0) from dual;
sign(123) sign(-100) sign(0)
--------- ---------- ---------
1 -1 0
30.sin
返回一个数字的正弦值
sql> select sin(1.57079) from dual;
sin(1.57079)
------------
1
? 31.sigh
返回双曲正弦的值
sql> select sin(20),sinh(20) from dual;
sin(20) sinh(20)
--------- ---------
.91294525 242582598
32.sqrt
返回数字n的根
sql> select sqrt(64),sqrt(10) from dual;
sqrt(64) sqrt(10)
--------- ---------
8 3.1622777
33.tan
返回数字的正切值
sql> select tan(20),tan(10) from dual;
tan(20) tan(10)
--------- ---------
2.2371609 .64836083
34.tanh
返回数字n的双曲正切值
sql> select tanh(20),tan(20) from dual;
tanh(20) tan(20)
--------- ---------
1 2.2371609
35.trunc
按照指定的精度截取一个数
sql> select trunc(124.1666,-2) trunc1,trunc(124.16666,2) from dual;
trunc1 trunc(124.16666,2)
--------- ------------------
100 124.16
36.add_months
增加或减去月份
sql> select to_char(add_months(to_date(199912,yyyymm),2),yyyymm) from dual;
to_cha
------
200002
sql> select to_char(add_months(to_date(199912,yyyymm),-2),yyyymm) from dual;
to_cha
------
199910
37.last_day
返回日期的最后一天
sql> select to_char(sysdate,yyyy.mm.dd),to_char((sysdate)+1,yyyy.mm.dd) from dual;
to_char(sy to_char((s
---------- ----------
2004.05.09 2004.05.10
sql> select last_day(sysdate) from dual;
last_day(s
----------
31-5月 -04
38.months_between(date2,date1)
给出date2-date1的月份
sql> select months_between(19-12月-1999,19-3月-1999) mon_between from dual;
mon_between
-----------
9
sql>selectmonths_between(to_date(2000.05.20,yyyy.mm.dd),to_date(2005.05.20,yyyy.mm.dd)) mon_betw from dual;
mon_betw
---------
-60
39.new_time(date,this,that)
给出在this时区=other时区的日期和时间
sql> select to_char(sysdate,yyyy.mm.dd hh24:mi:ss) bj_time,to_char(new_time
2 (sysdate,pdt,gmt),yyyy.mm.dd hh24:mi:ss) los_angles from dual;
bj_time los_angles
------------------- -------------------
2004.05.09 11:05:32 2004.05.09 18:05:32
40.next_day(date,day)
给出日期date和星期x之后计算下一个星期的日期
sql> select next_day('18-5月-2001','星期五') next_day from dual;
next_day
----------
25-5月 -01
41.sysdate
用来得到系统的当前日期
sql> select to_char(sysdate,dd-mm-yyyy day) from dual;
to_char(sysdate,
-----------------
09-05-2004 星期日
trunc(date,fmt)按照给出的要求将日期截断,如果fmt=mi表示保留分,截断秒
sql> select to_char(trunc(sysdate,hh),yyyy.mm.dd hh24:mi:ss) hh,
2 to_char(trunc(sysdate,mi),yyyy.mm.dd hh24:mi:ss) hhmm from dual;
hh hhmm
------------------- -------------------
2004.05.09 11:00:00 2004.05.09 11:17:00
42.chartorowid
将字符数据类型转换为rowid类型
sql> select rowid,rowidtochar(rowid),ename from scott.emp;
rowid rowidtochar(rowid) ename
------------------ ------------------ ----------
aaaafkaacaaaaeqaaa aaaafkaacaaaaeqaaa smith
aaaafkaacaaaaeqaab aaaafkaacaaaaeqaab allen
aaaafkaacaaaaeqaac aaaafkaacaaaaeqaac ward
aaaafkaacaaaaeqaad aaaafkaacaaaaeqaad jones
43.convert(c,dset,sset)
将源字符串 sset从一个语言字符集转换到另一个目的dset字符集
sql> select convert(strutz,we8hp,f7dec) "conversion" from dual;
conver
------
strutz
44.hextoraw
将一个十六进制构成的字符串转换为二进制
45.rawtohext
将一个二进制构成的字符串转换为十六进制
46.rowidtochar
将rowid数据类型转换为字符类型
47.to_char(date,format)
sql> select to_char(sysdate,yyyy/mm/dd hh24:mi:ss) from dual;
to_char(sysdate,yy
-------------------
2004/05/09 21:14:41
? 48.to_date(string,format)
将字符串转化为oracle中的一个日期
49.to_multi_byte
将字符串中的单字节字符转化为多字节字符
sql> select to_multi_byte(高) from dual;
to
--
高
50.to_number
将给出的字符转换为数字
sql> select to_number(1999) year from dual;
year
---------
1999
51.bfilename(dir,file)
指定一个外部二进制文件
sql>insert into file_tb1 values(bfilename(lob_dir1,image1.gif));
52.convert(x,desc,source)
将x字段或变量的源source转换为desc
sql> select sid,serial#,username,decode(command,
2 0,none,
3 2,insert,
4 3,
5 select,
6 6,update,
7 7,delete,
8 8,drop,
9 other) cmd from v$session where type!=background;
sid serial# username cmd
--------- --------- ------------------------------ ------
1 1 none
2 1 none
3 1 none
4 1 none
5 1 none
6 1 none
7 1275 none
8 1275 none
9 20 gao select
10 40 gao none
53.dump(s,fmt,start,length)
dump函数以fmt指定的内部数字格式返回一个varchar2类型的值
sql> col global_name for a30
sql> col dump_string for a50
sql> set lin 200
sql> select global_name,dump(global_name,1017,8,5) dump_string from global_name;
global_name dump_string
------------------------------ --------------------------------------------------
oracle.world typ=1 len=12 characterset=zhs16gbk: w,o,r,l,d
54.empty_blob()和empty_clob()
这两个函数都是用来对大数据类型字段进行初始化操作的函数
55.greatest
返回一组表达式中的最大值,即比较字符的编码大小.
sql> select greatest(aa,ab,ac) from dual;
gr
--
ac
sql> select greatest(啊,安,天) from dual;
gr
--
天
56.least
返回一组表达式中的最小值
sql> select least(啊,安,天) from dual;
le
--
啊
57.uid
返回标识当前用户的唯一整数
sql> show user
user 为"gao"
sql> select username,user_id from dba_users where user_id=uid;
username user_id
------------------------------ ---------
gao 25
58.user
返回当前用户的名字
sql> select user from dual;
user
------------------------------
gao
59.userevn
返回当前用户环境的信息,opt可以是:
entryid,sessionid,terminal,isdba,lable,language,client_info,lang,vsize
isdba 查看当前用户是否是dba如果是则返回true
sql> select userenv(isdba) from dual;
useren
------
false
sql> select userenv(isdba) from dual;
useren
------
true
session
返回会话标志
sql> select userenv(sessionid) from dual;
userenv(sessionid)
--------------------
152
entryid
返回会话人口标志
sql> select userenv(entryid) from dual;
userenv(entryid)
------------------
0
instance
返回当前instance的标志
sql> select userenv(instance) from dual;
userenv(instance)
-------------------
1
language
返回当前环境变量
sql> select userenv(language) from dual;
userenv(language)
----------------------------------------------------
simplified chinese_china.zhs16gbk
lang
返回当前环境的语言的缩写
sql> select userenv(lang) from dual;
userenv(lang)
----------------------------------------------------
zhs
terminal
返回用户的终端或机器的标志
sql> select userenv(terminal) from dual;
userenv(termina
----------------
gao
vsize(x)
返回x的大小(字节)数
sql> select vsize(user),user from dual;
vsize(user) user
----------- ------------------------------
6 system
? 60.avg(distinct|all)
all表示对所有的值求平均值,distinct只对不同的值求平均值
sqlwks> create table table3(xm varchar(8),sal number(7,2));
语句已处理。
sqlwks> insert into table3 values(gao,1111.11);
sqlwks> insert into table3 values(gao,1111.11);
sqlwks> insert into table3 values(zhu,5555.55);
sqlwks> commit;
sql> select avg(distinct sal) from gao.table3;
avg(distinctsal)
----------------
3333.33
sql> select avg(all sal) from gao.table3;
avg(allsal)
-----------
2592.59
61.max(distinct|all)
求最大值,all表示对所有的值求最大值,distinct表示对不同的值求最大值,相同的只取一次
sql> select max(distinct sal) from scott.emp;
max(distinctsal)
----------------
5000
62.min(distinct|all)
求最小值,all表示对所有的值求最小值,distinct表示对不同的值求最小值,相同的只取一次
sql> select min(all sal) from gao.table3;
min(allsal)
-----------
1111.11
63.stddev(distinct|all)
求标准差,all表示对所有的值求标准差,distinct表示只对不同的值求标准差
sql> select stddev(sal) from scott.emp;
stddev(sal)
-----------
1182.5032
sql> select stddev(distinct sal) from scott.emp;
stddev(distinctsal)
-------------------
1229.951
64.variance(distinct|all)
求协方差
sql> select variance(sal) from scott.emp;
variance(sal)
-------------
1398313.9
65.group by
主要用来对一组数进行统计
sql> select deptno,count(*),sum(sal) from scott.emp group by deptno;
deptno count(*) sum(sal)
--------- --------- ---------
10 3 8750
20 5 10875
30 6 9400
66.having
对分组统计再加限制条件
sql> select deptno,count(*),sum(sal) from scott.emp group by deptno having count(*)>=5;
deptno count(*) sum(sal)
--------- --------- ---------
20 5 10875
30 6 9400
sql> select deptno,count(*),sum(sal) from scott.emp having count(*)>=5 group by deptno ;
deptno count(*) sum(sal)
--------- --------- ---------
20 5 10875
30 6 9400
67.order by
用于对查询到的结果进行排序输出
sql> select deptno,ename,sal from scott.emp order by deptno,sal desc;
deptno ename sal
--------- ---------- ---------
10 king 5000
10 clark 2450
10 miller 1300
20 scott 3000
20 ford 3000
20 jones 2975
20 adams 1100
20 smith 800
30 blake 2850
30 allen 1600
30 turner 1500
30 ward 1250
30 martin 1250
30 james 950
68. pl/sql中的case语句
select (case when dummy='x' then 0 else 1 end) as flag from dual;
case的第1种用法:
case col when 'a' then 1
when 'b' then 2
else 0 end
这种用法跟decode一样没什么区别
case的第2种用法:
case when score <60 then 'd'
when score >=60 and score <70 then 'c'
when score >=70 and score <80 then 'b'
else 'a' end
69.nvl(expr1, expr2)
nvl(expr1, expr2)->expr1为null,返回expr2;不为null,返回expr1。注意两者的类型要一致
nvl2 (expr1, expr2, expr3) ->expr1不为null,返回expr2;为null,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型
nullif (expr1, expr2) ->相等返回null,不等返回expr1
? oracle分析函数参考手册
=============================================
作者: xsb([url]http://xsb.itpub.net)[/url]
发表于:2006.03.01 12:22
分类: dw&bi
出处:http://xsb.itpub.net/post/419/33028
---------------------------------------------------------------
oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,
而聚合函数对于每个组只返回一行。
常用的分析函数如下所列:
row_number() over(partition by ... order by ...)
rank() over(partition by ... order by ...)
dense_rank() over(partition by ... order by ...)
count() over(partition by ... order by ...)
max() over(partition by ... order by ...)
min() over(partition by ... order by ...)
sum() over(partition by ... order by ...)
avg() over(partition by ... order by ...)
first_value() over(partition by ... order by ...)
last_value() over(partition by ... order by ...)
lag() over(partition by ... order by ...)
lead() over(partition by ... order by ...)
下面例子中使用的表来自oracle自带的hr用户下的表,如果没有安装该用户,可以在sys用户下运行
$oracle_home/demo/schema/human_resources/hr_main.sql来创建。
除本文内容外,你还可参考:
rollup与cube http://xsb.itpub.net/post/419/29159
分析函数使用例子介绍:http://xsb.itpub.net/post/419/44634
本文如果未指明,缺省是在hr用户下运行例子。
开窗函数的的理解:
开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数
over(partition by deptno)按照部门分区
over(order by salary range between 50 preceding and 150 following)
每行对应的数据窗口是之前行幅度值不超过50,之后行幅度值不超过150
over(order by salary rows between 50 preceding and 150 following)
每行对应的数据窗口是之前50行,之后150行
over(order by salary rows between unbounded preceding and unbounded following)
每行对应的数据窗口是从第一行到最后一行,等效:
over(order by salary range between unbounded preceding and unbounded following)
主要参考资料:《expert one-on-one》 tom kyte 《oracle9i sql reference》第6章
ohwww 2007-3-12 09:19
续