oracle-常用函数
?
?
dual表可以用了查询很多。
1.ascii(str)//返回与指定的字符对应的十进制数;
? ? ? ? ?select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space from dual;
?
2.chr(int)//给出整数,返回对应的字符;
? ? ? ? ?select chr(54740) zhao,chr(65) chr65 from dual;
?
3.concat(s1,s2)连接两个字符串;
? ? ? ? select concat('010-','88888888')||'转23' ?电话别名 from dual;
?
4.initcap(str)返回字符串并将字符串的第一个字母变为大写;
? ? ? ? select initcap('smith') upp from dual;
?
5.instr(str,sub_str,start_index,frequency)
? ? ? ? select instr('oracle traning','ra',1,2) instring from dual;
?
6.length(str)返回字符串的长度;
?
7.lower(str)返回字符串,并将所有的字符小写
?
8.upper(str)返回字符串,并将所有的字符大写
?
9.rpad(右填充字符)和lpad(左填充字符)
? ? ? ? select lpad(rpad('gao',5,'*'),8,'*')from dual;
? ? ? ? 结果:***gao**
? ? ? ? gao**//5个字符;***gao**//8个字符
?
10.ltrim(str),rtrim(str),trim(str)
?
11.substr(string,start,count)取子字符串,从start开始,取count个
? ? ? ? ?select substr('13088888888',3,8) from dual;
?
12.replace('string','s1','s2')
? ? ? ? string ? 希望被替换的字符或变量?
? ? ? ? s1 ? ? ? 被替换的字符串
? ? ? ? s2 ? ? ? 要替换的字符串
? ? ? ? select replace('he love you','he','i') from dual;
?
13.abs()绝对值
? ? ? ? mod,acos(),sign()符号函数,avg(),协方差(),等一系列数学函数。
14.四舍五入
? ? ? ? cell()返回大于或等于给出数字的最小整数
? ? ? ? ? ? ? ? select ceil(3.1415927) from dual;
? ? ? ? floor()对给定的数字取整数
? ? ? ? ? ? ? ? select floor(2345.67) from dual;
? ? ? ? ROUND和TRUNC
? ? ? ? ? ? ? ? round()//往上靠
? ? ? ? ? ? ? ? trunc()//向0靠//截取
?
按照指定的精度进行舍入
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
?
?
15.日期<-->字符串
? ? ? ? add_months()增加或减去月份
? ? ? ? ? ? ? ? select to_char(add_months(to_date('199912','yyyymm'),2),'yyyymm') from dual;
? ? ? ? to_char()
? ? ? ? ? ? ? ? to_char(add_months(to_date('199912','yyyymm'),-2),'yyyymm') from dual;
? ? ? ? to_date()--补充
?
?
? ? ? ? ? ? ? ? select last_day(sysdate) from dual;
?
? ? ? ? 给出date2-date1的月份
? ? ? ? ? ? ? ? select months_between('19-12月-1999','19-3月-1999') mon_between from dual;
?
? ? ? ? select next_day('18-5月-2001','星期五') next_day from dual;
?
? ? ? ? sysdate用来得到系统的当前日期
? ? ? ? ? ? ? ? select to_char(sysdate,'dd-mm-yyyy day') from dual;
?
16.convert(c,dset,sset)将源字符串 sset从一个语言字符集转换到另一个目的dset字符集
? ? ? ? select convert('strutz','we8hp','f7dec') "conversion" from dual;
? ? ? ? 以及各种进制之间的转换
?
17.to_number(str)将给出的字符转换为数字
? ? ? ? select to_number('1999') year from dual;
?
18.bfilename(dir,file)指定一个外部二进制文件
? ? ? ? insert into file_tb1 values(bfilename('lob_dir1','image1.gif'));
?
19.uid返回标识当前用户的唯一整数
SQL> show user
USER 为"GAO"
? ? ? ? select username,user_id from dba_users where user_id=uid;
?
20.user返回当前用户的名字
? ? ? ? select user from ?dual;
?
21.userenv(opt)返回当前用户环境的信息,opt可以是:
? ? ? ? ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CLIENT_INFO,LANG,VSIZE等
? ? ? ? 返回当前INSTANCE的标志
? ? ? ? select userenv('instance') from dual;
? ? ? ? select userenv('language') from dual;
? ? ? ? select userenv('terminal') from dual;