ORACLE 常用函数总结(80个)第1/2页
程序员文章站
2023-12-05 08:11:40
1. ascii 返回与指定的字符对应的十进制数; sql> select ascii(a) a,ascii(a) a,ascii(0) zero,ascii( )...
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
19.ceil
返回大于或等于给出数字的最小整数
sql> select ceil(3.1415927) from dual;
ceil(3.1415927)
---------------
4
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
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
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
返回与指定的字符对应的十进制数;
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
19.ceil
返回大于或等于给出数字的最小整数
sql> select ceil(3.1415927) from dual;
ceil(3.1415927)
---------------
4
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
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
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
1