oracle 50個函數總結

--1.ASCII返回與指定的字符對應的十進制數;
select ascii('A') A, ascii('a') a, ascii('0') zero, ascii(' ') space
from dual;
--返回: 65 97 48 32

--2.CHR給出整數,返回對應的字符;
select chr(54740) zhao, chr(65) chr65 from dual;
--返回:趙 A

--3.CONCAT連接兩個字符串;
select concat('010-', '88888888') || '轉23' 高乾競電話 from dual;
--返回:010-88888888轉23

--4.INITCAP返回字符串並將字符串的第一個字母變爲大寫;
select initcap('smith') upp from dual;
--返回:Smith

--5.INSTR(C1,C2,I,J)在一個字符串中搜索指定的字符,返回發現指定的字符的位置;
--C1 被搜索的字符串
--C2 希望搜索的字符串
--I 搜索的開始位置,默認爲1
--J 出現的位置,默認爲1
select instr('oracle traning', 'ra', 1, 2) instring from dual;
--返回: 9

--6.LENGTH返回字符串的長度;
select name, length(name), addr, length(addr), sal, length(to_char(sal))
from gao.nchar_tst;
--返回:高乾競 3 北京市海錠區 6 9999.99 7

--7.LOWER返回字符串,並將所有的字符小寫
select lower('AaBbCcDd') AaBbCcDd from dual;
--返回:aabbccdd

--8.UPPER返回字符串,並將所有的字符大寫
select upper('AaBbCcDd') upper from dual;
--返回:AABBCCDD

--9.RPAD和LPAD(粘貼字符)RPAD 在列的右邊粘貼字符LPAD 在列的左邊粘貼字符
select lpad(rpad('gao', 10, '*'), 17, '*') from dual;
--返回:*******gao*******

--10.LTRIM和RTRIMLTRIM 刪除左邊出現的字符串RTRIM 刪除右邊出現的字符串
select ltrim(rtrim(' gao qian jing ', ' '), ' ') from dual;
--返回:gao qian jing

--11.SUBSTR(string,start,count)取子字符串,從start開始,取count個
select substr('13088888888', 3, 8) from dual;
--返回:08888888

--12.REPLACE('string','s1','s2')
--string 希望被替換的字符或變量
--s1 被替換的字符串
--s2 要替換的字符串
select replace('he love you','he','i') from dual;
--返回:i love you

--13.SOUNDEX返回一個與給定的字符串讀音相同的字符串
create table table1(xm varchar(8));
insert into table1 values('weather');
insert into table1 values('wether');
insert into table1 values('gao');
select xm from table1 where soundex(xm)=soundex('weather');
--返回結果:weather wether

--14.剪掉某個的字符,如果不指定,默認刪除空格
select TRIM('s' from 'string') from dual;
select trim(' string') from dual;
select ltrim(' string') from dual;
select rtrim(' string ') from dual;

--15.ABS返回指定值的絕對值
select abs(100),abs(-100) from dual;
--返回結果: 100 100

--16.ACOS給出反餘弦的值
select acos(-1) from dual;
--返回結果:3.1415927

--17.ASIN給出反正弦的值
select asin(0.5) from dual;
--返回結果:0.523598775598299

--18.ATAN返回一個數字的反正切值
select atan(1) from dual;
--返回結果:0.785398163397448

--19.CEIL返回大於或等於給出數字的最小整數
select ceil(3.1415927) from dual;
--返回結果:4

--20.COS返回一個給定數字的餘弦
select cos(-3.1415927) from dual;
--返回結果:-0.999999999999999

--21.COSH返回一個數字反餘弦值
select cosh(20) from dual;
--返回結果:242582597.704895

--22.EXP返回一個數字e的n次方根
select exp(2),exp(1) from dual;
--返回結果:7.38905609893065 2.71828182845905

--23.FLOOR對給定的數字取整數
select floor(2345.67) from dual;
--返回結果:2345

--24.LN返回一個數字的對數值
select ln(1),ln(2),ln(2.7182818) from dual;
--返回結果:0 0.693147180559945 0.999999989530502

--25.LOG(n1,n2)返回一個以n1爲底n2的對數
select log(2,1),log(2,4) from dual;
--返回結果:0 2

--26.MOD(n1,n2)返回一個n1除以n2的餘數
select mod(10,3),mod(3,3),mod(2,3) from dual;
--返回結果 : 1 0 2

--27.POWER返回n1的n2次方根
select power(2,10),power(3,3) from dual;
--返回結果:1024 27

--28.ROUND和TRUNC按照指定的精度進行舍入
select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
--返回結果:56 -55 55 -55

--29.SIGN取數字n的符號,大於0返回1,小於0返回-1,等於0返回0
select sign(123),sign(-100),sign(0) from dual;
--返回結果: 1 -1 0

--30.SIN返回一個數字的正弦值
select sin(1.57079) from dual;
--返回結果:0.999999999979986

--31.SIGH返回雙曲正弦的值
select sin(20),sinh(20) from dual;
--返回結果:0.912945250727628 242582597.704895

--32.SQRT返回數字n的根
select sqrt(64),sqrt(10) from dual;
--返回結果:8 3.16227766016838

--33.TAN返回數字的正切值
select tan(20),tan(10) from dual;
--返回結果:2.23716094422474 0.648360827459087

--34.TANH返回數字n的雙曲正切值
select tanh(20),tan(20) from dual;
--返回結果:1 2.23716094422474

--35.TRUNC按照指定的精度截取一個數
select trunc(124.1666,-2) trunc1,trunc(124.16666,2) from dual;
--返回結果:100 124.16

--36.ADD_MONTHS增加或減去月份
select to_char(add_months(to_date('199912','yyyymm'),2),'yyyymm') from dual;
--返回結果:200002
select to_char(add_months(to_date('199912','yyyymm'),-2),'yyyymm') from dual;
--返回結果:199910

--37.LAST_DAY返回日期的最後一天
select to_char(sysdate,'yyyy.mm.dd'),to_char((sysdate)+1,'yyyy.mm.dd') from dual;
--2010.09.14 2010.09.15
select last_day(sysdate) from dual;
--2010-9-30 下午 04:48:10

--38.MONTHS_BETWEEN(date2,date1)給出date2-date1的月份
select months_between('19-12月-1999','19-3月-1999') mon_between from dual;
--返回結果:9
select months_between(to_date('2000.05.20','yyyy.mm.dd'),to_date('2005.05.20','yyyy.mm.dd')) mon_betw from dual;
--返回結果:-60

--39.NEW_TIME(date,'this','that')給出在this時區=other時區的日期和時間
select to_char(sysdate,'yyyy.mm.dd hh24:mi:ss') bj_time,to_char(new_time(sysdate,'PDT','GMT'),'yyyy.mm.dd hh24:mi:ss') los_angles from dual;
--返回結果:2010.09.14 16:53:25 2010.09.14 23:53:25

--40.NEXT_DAY(date,'day')給出日期date和星期x之後計算下一個星期的日期
select next_day('18-5月-2001','星期五') next_day from dual;

--41.SYSDATE用來得到系統的當前日期
select to_char(sysdate,'dd-mm-yyyy day') from dual;
--返回結果:14-09-2010 星期二

--trunc(date,fmt)按照給出的要求將日期截斷,如果fmt='mi'表示保留分,截斷秒
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;
--返回結果:2010.09.14 16:00:00 2010.09.14 16:56:00

--42.chartorowid將字符數據類型轉換爲ROWID類型
select rowid,rowidtochar(rowid),ename from scott.emp;

--43.CONVERT(c,dset,sset)將源字符串 sset從一個語言字符集轉換到另一個目的dset字符集
select convert('strutz','we8hp','f7dec') "conversion" from dual;
--返回結果:strutz

--44.TO_CHAR(date,'format')
select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;
--返回結果 : 2010/09/14 17:26:24

--45.TO_DATE(string,'format')將字符串轉化爲ORACLE中的一個日期
select to_date('2010-05-10','yyyy-mm-dd') FROM dual;
--返回:2010-5-10

--46.TO_MULTI_BYTE將字符串中的單字節字符轉化爲多字節字符
select to_multi_byte('高') from dual;

--47.TO_NUMBER將給出的字符轉換爲數字
select to_number('1999') year from dual;
--返回:1999

--48.GREATEST返回一組表達式中的最大值,即比較字符的編碼大小
select greatest('AA','AB','AC') from dual;
--返回:AC
select greatest('啊','安','天') from dual;
--返回:天

--49.LEAST返回一組表達式中的最小值
select least('啊','安','天') from dual;
--返回:啊

--50.USER返回當前用戶的名字
select user from dual;
--返回:scott

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章