Oracle函數小解

SQL中的單記錄函數
1.ASCII
返回與指定的字符對應的十進制數;
SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space from dual;
2.CHR
給出整數,返回對應的字符;
SQL> select chr(54740) zhao,chr(65) chr65 from dual;
3.CONCAT 等價與||,推薦CONCAT
連接兩個字符串;
SQL> select concat('010-','88888888')||'轉23' 高乾競電話 from dual;
4.INITCAP
返回字符串並將字符串的第一個字母變爲大寫;
SQL> select initcap('smith') upp from dual;
5.INSTR(C1,C2,I,J)和INSTRB(C1,C2,I,J)
在一個字符串中搜索指定的字符,返回發現指定的字符的位置;
C1    被搜索的字符串
C2    希望搜索的字符串
I     搜索的開始位置,默認爲1
J     出現的位置,默認爲1
SQL> select instr('oracle traning','ra',1,2) instring from dual;
6.LENGTH和LENGTHB
返回字符串的長度; LENGTHB按照字節進行返回
SQL> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from gao.nchar_tst;
7.LOWER
返回字符串,並將所有的字符小寫
SQL> select lower('AaBbCcDd')AaBbCcDd from dual;
8.UPPER
返回字符串,並將所有的字符大寫
SQL> select upper('AaBbCcDd') upper from dual;
9.RPAD和LPAD(粘貼字符) 字符串填充
RPAD 在列的右邊粘貼字符
LPAD 在列的左邊粘貼字符
SQL> select lpad(rpad('gao',10,'*'),17,'*')from dual;
10.LTRIM和RTRIM
LTRIM 刪除左邊出現的字符串
RTRIM 刪除右邊出現的字符串
SQL> select ltrim(rtrim('   gao qian jing   ',' '),' ') from dual;
11.SUBSTR(string,start,count)和SUBSTRB(string,start,count)
取子字符串,從start開始,取count個
SQL> select substr('13088888888',3,8) from dual;
12.REPLACE('string','s1','s2')
string   希望被替換的字符或變量
s1       被替換的字符串
s2       要替換的字符串
SQL> select replace('he love you','he','i') from dual;
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');
14.TRIM('s' from 'string')
LEADING   剪掉前面的字符
TRAILING 剪掉後面的字符
如果不指定,默認爲空格符
15.ABS
返回指定值的絕對值
SQL> select abs(100),abs(-100) from dual;
16.ACOS
給出反餘弦的值
SQL> select acos(-1) from dual;
17.ASIN
給出反正弦的值
SQL> select asin(0.5) from dual;
18.ATAN
返回一個數字的反正切值
SQL> select atan(1) from dual;
19.CEIL -- FLOOR
返回大於或等於給出數字的最小整數
SQL> select ceil(3.1415927) from dual;
20.COS
返回一個給定數字的餘弦
SQL> select cos(-3.1415927) from dual;
21.COSH
返回一個數字反餘弦值
SQL> select cosh(20) from dual;
22.EXP
返回一個數字e的n次方根
SQL> select exp(2),exp(1) from dual;
23.FLOOR
對給定的數字取整數,返回小於或等於給定數的最大整數與CEIL相反
SQL> select floor(2345.67) from dual;
24.LN
返回一個數字的對數值
SQL> select ln(1),ln(2),ln(2.7182818) from dual;
25.LOG(n1,n2)
返回一個以n1爲底n2的對數
SQL> select log(2,1),log(2,4) from dual;
26.MOD(n1,n2)
返回一個n1除以n2的餘數
SQL> select mod(10,3),mod(3,3),mod(2,3) from dual;
27.POWER
返回n1的n2次方根
SQL> select power(2,10),power(3,3) from dual;
28.ROUND和TRUNC(x,m,n)
按照指定的精度進行舍入
SQL> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
29.SIGN 符號函數
取數字n的符號,大於0返回1,小於0返回-1,等於0返回0
SQL> select sign(123),sign(-100),sign(0) from dual;
 
30.SIN
返回一個數字的正弦值
SQL> select sin(1.57079) from dual;
31.SIGH
返回雙曲正弦的值
SQL> select sin(20),sinh(20) from dual;
32.SQRT
返回數字n的根
SQL> select sqrt(64),sqrt(10) from dual;
33.TAN
返回數字的正切值
SQL> select tan(20),tan(10) from dual;
34.TANH
返回數字n的雙曲正切值
SQL> select tanh(20),tan(20) from dual;
35.TRUNC
按照指定的精度截取一個數
SQL> select trunc(124.1666,-2) trunc1,trunc(124.16666,2) from dual;
36.ADD_MONTHS
增加或減去月份
SQL> select to_char(add_months(to_date('199912','yyyymm'),2),'yyyymm') from dual;
SQL> select to_char(add_months(to_date('199912','yyyymm'),-2),'yyyymm') from dual;
37.LAST_DAY
返回日期的最後一天
SQL> select to_char(sysdate,'yyyy.mm.dd'),to_char((sysdate)+1,'yyyy.mm.dd') from dual;
SQL> select last_day(sysdate) from dual;
38.MONTHS_BETWEEN(date2,date1)
給出date2-date1的月份
SQL> select months_between('19-12月-1999','19-3月-1999') mon_between from dual;
SQL>selectmonths_between(to_date('2000.05.20','yyyy.mm.dd'),to_date('2005.05.20','yyyy.mm.dd')) mon_betw from dual;
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;
40.NEXT_DAY(date,'day')
給出日期date和星期x之後計算下一個星期的日期
SQL> select next_day('18-5月-2001','星期五') next_day from dual;
41.SYSDATE
用來得到系統的當前日期
SQL> select to_char(sysdate,'dd-mm-yyyy day') from dual;
42.CHARTOROWID
將字符數據類型轉換爲ROWID類型
SQL> select rowid,rowidtochar(rowid),ename from scott.emp;
43.CONVERT(c,dset,sset)
將源字符串 sset從一個語言字符集轉換到另一個目的dset字符集
SQL> select convert('strutz','we8hp','f7dec') "conversion" from dual;
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;
48.TO_DATE(string,'format')
將字符串轉化爲ORACLE中的一個日期
49.TO_MULTI_BYTE
將字符串中的單字節字符轉化爲多字節字符
SQL>  select to_multi_byte('高') from dual;
50.TO_NUMBER
將給出的字符轉換爲數字
SQL> select to_number('1999') year from dual;
51.BFILENAME(dir,file)
指定一個外部二進制文件
SQL>insert into file_tb1 values(bfilename('lob_dir1','image1.gif'));
52.CONVERT('x','desc','source')
將x字段或變量的源source轉換爲desc
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;
54.EMPTY_BLOB()和EMPTY_CLOB()
這兩個函數都是用來對大數據類型字段進行初始化操作的函數
55.GREATEST
返回一組表達式中的最大值,即比較字符的編碼大小.
SQL> select greatest('AA','AB','AC') from dual;
SQL> select greatest('啊','安','天') from dual;
56.LEAST
返回一組表達式中的最小值
SQL> select least('啊','安','天') from dual;
57.UID
返回標識當前用戶的唯一整數
SQL> show user
SQL> select username,user_id from dba_users where user_id=uid;
58.USER
返回當前用戶的名字
SQL> select user from dual;
59.USEREVN
返回當前用戶環境的信息,opt可以是:
ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CLIENT_INFO,LANG,VSIZE
ISDBA 查看當前用戶是否是DBA如果是則返回true
SQL> select userenv('isdba') from dual;
SESSION
返回會話標誌
SQL> select userenv('sessionid') from dual;
ENTRYID
返回會話人口標誌
SQL> select userenv('entryid') from dual;
INSTANCE
返回當前INSTANCE的標誌
SQL> select userenv('instance') from dual;
LANGUAGE
返回當前環境變量
SQL> select userenv('language') from dual;
LANG
返回當前環境的語言的縮寫
SQL> select userenv('lang') from dual;
TERMINAL
返回用戶的終端或機器的標誌
SQL> select userenv('terminal') from dual;
VSIZE(X)
返回X的大小(字節)數
SQL> select vsize(user),user from dual;
60.AVG(DISTINCT|ALL)
all表示對所有的值求平均值,distinct只對不同的值求平均值
SQL> create table table3(xm varchar(8),sal number(7,2));
SQL> insert into table3 values('gao',1111.11);
SQL> insert into table3 values('gao',1111.11);
SQL> insert into table3 values('zhu',5555.55);
SQL> commit;
SQL> select avg(distinct sal) from table3;
SQL> select avg(all sal) from gao.table3;
61.MAX(DISTINCT|ALL)
求最大值,ALL表示對所有的值求最大值,DISTINCT表示對不同的值求最大值,相同的只取一次
SQL> select max(distinct sal) from scott.emp;
62.MIN(DISTINCT|ALL)
求最小值,ALL表示對所有的值求最小值,DISTINCT表示對不同的值求最小值,相同的只取一次
SQL> select min(all sal) from gao.table3;
63.STDDEV(distinct|all)
求標準差,ALL表示對所有的值求標準差,DISTINCT表示只對不同的值求標準差
SQL> select stddev(sal) from scott.emp;
SQL> select stddev(distinct sal) from scott.emp;
64.VARIANCE(DISTINCT|ALL)
求協方差
SQL> select variance(sal) from scott.emp;
65.GROUP BY
主要用來對一組數進行統計
SQL> select deptno,count(*),sum(sal) from scott.emp group by deptno;
66.HAVING
對分組統計再加限制條件
SQL> select deptno,count(*),sum(sal) from scott.emp group by deptno having count(*)>=5;
SQL> select deptno,count(*),sum(sal) from scott.emp having count(*)>=5 group by deptno ;
67.ORDER BY
用於對查詢到的結果進行排序輸出
SQL> select deptno,ename,sal from scott.emp order by deptno,sal desc;
68.DECODE
 條件判斷賦值,很有用處,相當於IF判斷。可以簡化查詢、數據轉換、提高性能等功效
 DECODE(expr,adjust1,value1,……,default value)
 Select User_no,Decode(User_type1,’10’,’專變戶’,’20’,’公變戶’,’考覈戶’) From User_files Where Business_Place_code=’09142’
69.NVL
 空值判斷
 Select Nvl(write_sect_no,’無抄表區段’) from user_files
70.SUM
 求和
 Select Sum(Total_power) from df_money_files where total_money >0
71.to_char(n[,fmt]) 數字轉換成字符
其實:to_number,to_char,to_date等轉換函數都可以在很多數據類型之間進行轉換,to_lob一般只能將long、long raw轉換爲clob、blob、nclob類型
數字格式:
 
72.round/trunc(date,fmt)
按照給出的要求將日期截斷,如果fmt='mi'表示保留分,截斷秒
SQL> select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') hh,
    to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from dual;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章