一、字符函數(shù):
1.concat函數(shù):用來拼接2個(gè)字符串,Expression:concat(str1,str2);
Example:
select concat('milktea','leaf') from dual;
結(jié)果如圖:
注意:concat函數(shù)不能處理2個(gè)以上的字符串拼接,否則會(huì)報(bào)參數(shù)個(gè)數(shù)無效的錯(cuò)誤。
select concat('milktea','leaf','third') from dual;
2、substr函數(shù):用于截取字符串,從指定位置index截取指定長(zhǎng)度length的字符串。Expression:substr(str,index,length);
Example:
select substr('milktealeaf',5,3) from dual;
結(jié)果如圖:
注意:此處字符串的下標(biāo)從1開始,index的值是幾就是第幾個(gè)。
另外還有一個(gè)substrb函數(shù),和substr函數(shù)作用相同,但2者稍有差別:substr按字符取,而substrb按字節(jié)取。結(jié)論顯而易見:
在取英文字母時(shí),2者相同,但在取漢字時(shí),一個(gè)漢字2個(gè)字節(jié),如果是奇數(shù)個(gè)字節(jié)則自動(dòng)舍棄最后一位,示例如下:
select substr('milktealeaf',2,5) from dual;
select substrb('milktealeaf',2,5) from dual;
select substr('李先森陳小姐你們好呀',2,5) from dual;
select substrb('李先森陳小姐你們好呀',2,5) from dual;
3、Instr函數(shù):用于獲取一個(gè)字符串中子串str_son在字符串str_father中從index下標(biāo)查找第times次出現(xiàn)的位置,返回一個(gè)下標(biāo)
Expression:(str_father,str_son,index,times)
Example:
select instr('milkteatealeaftea','tea',3,2) from dual;
注:如圖所示。第二個(gè)tea在下標(biāo)為8處。
另外,也有一個(gè)instrb函數(shù),同substr和substrb函數(shù)一樣,instr用于字符,instrb用于字節(jié),示例如下:
select instr('李先森陳女士李先森李先森陳女士','李先森',3,2) from dual;
select instrb('李先森陳女士李先森李先森陳女士','李先森',3,2) from dual;
4、length函數(shù):返回一個(gè)字符串的長(zhǎng)度 Expression:length(str);
Example:
select length('milktea') from dual
5、lpad函數(shù):用于對(duì)字符串左側(cè)進(jìn)行填充的函數(shù),l即為left,Expression:lpad(str1,length,str2)其中str1為被填充的字符串,length為填充以后的長(zhǎng)度,str2為要填充的字符。
Example:
select lpad('leaf',9,'%*') from dual;
注:從左側(cè)開始填充,只要長(zhǎng)度達(dá)到立即停止,不管要填充的字符串有沒有填充完整。有了左填充,必然有右填充rpad,r即為right,從字符串右側(cè)進(jìn)行填充。示例如下:
select rpad('leaf',9,'%*') from dual;
6、replace函數(shù):顧名思義,替換函數(shù),將字符串中的子串用其他字符進(jìn)行替換。Expression:replace(str_father,str_son,str_replace);
Example:
select replace('milkteathird','third','leaf') from dual;
7、ltrim函數(shù):去掉字符串左側(cè)的空白。 Expression:ltrim(str)
Example:
select ltrim(' milktea leaf ') from dual;
可以看到左側(cè)空白已經(jīng)去除,那么同樣的有rtrim函數(shù),去掉右側(cè)空白。去空白,那么當(dāng)然左右2側(cè)都去也可以,trim函數(shù)。示例如下:
select rtrim(' milktea leaf ') from dual;
select trim(' milktea leaf ') from dual;
二、數(shù)值函數(shù):
1、round函數(shù):用于對(duì)一個(gè)數(shù)取四舍五入后的結(jié)果。Expression(number,m) number為數(shù)字,m為要四舍五入的位數(shù),必須為整數(shù)。
Example:
select round(13.14,1) from dual;
select round(13.14,-1) from dual;
可以看出,當(dāng)位數(shù)為正數(shù)時(shí),保存小數(shù)點(diǎn)后面的位數(shù),負(fù)數(shù)時(shí)直接取整數(shù)部分的四舍五入。
2、trunc函數(shù):直接進(jìn)行截取數(shù)字,不進(jìn)行四舍五入。Expression: trunc(number,m)number為截取的數(shù)字,m為截取的位數(shù)。
Example:
select trunc(15.15,1) from dual;
select trunc(15.15,-1) from dual;
并沒有進(jìn)行四舍五入,直接截取,毫不留情。
3、mod函數(shù):對(duì)2個(gè)數(shù)進(jìn)行取模運(yùn)算,如果除數(shù)為0 ,則返回被除數(shù)。Expression:mod(number1,number2)
Example:
select mod(5,2) from dual;
select mod(3,0) from dual;
4、ceil函數(shù):用于返回大于等于當(dāng)前數(shù)的最小整數(shù) Expression:ceil(number)
Example:
select ceil(13.14) from dual;
5、floor函數(shù):和ceil函數(shù)相反,用于返回小于等于當(dāng)前數(shù)的最大整數(shù) Expression:floor(number)
Example:
select floor(13.14) from dual;
6、abs函數(shù):返回當(dāng)前數(shù)的絕對(duì)值:Expression:abs(number)
Example:
select abs(-1314) from dual;
7、cos函數(shù):返回當(dāng)前數(shù)的余弦值 Expression:cos(number)
Example:
三、日期函數(shù):
1、sysdate函數(shù):返回系統(tǒng)當(dāng)前日期 Expression :sysdate
Example:
select sysdate from dual;
2、round函數(shù):對(duì)日期取四舍五入的結(jié)果,如果精度是日的話以每周周四為標(biāo)準(zhǔn),精度是月以每月16日為標(biāo)準(zhǔn),精度是年以每年7月1日為標(biāo)準(zhǔn)。 分別四舍五入到最近的周日,每月1號(hào),以及每年1月1日, Expression:round(date,'format')
Example:
select round(sysdate,'day') from dual;
select round(sysdate,'month') from dual;
select round(sysdate,'year') from dual;
可以看到現(xiàn)在的時(shí)間過了周三,取最近的周日8月5日,按月份還沒過16日,取每月1日,按年份已經(jīng)過了7月1日,取下一年的1月1日。
3、trunc函數(shù):截取日期,精度為日,取當(dāng)前星期的第一天(按第一天是周日算),精度為月,按每月第一日算,精度為年,按每年1月1日算。Expression:trunc(date,'format')
Example:
select trunc(sysdate,'day') from dual;
select trunc(sysdate,'month') from dual;
select trunc(sysdate,'year') from dual;
今天周六,取上周日7月29,按月份取本月1日,按年份取今年1月1日。
4、add_months函數(shù):在日期的基礎(chǔ)上加減整月后的日期,Expression:add_months(date,number)其中date為日期,number為整數(shù)。
Example:
select add_months(sysdate,3) from dual;
select add_months(sysdate,-3) from dual;
5、months_between函數(shù):返回2個(gè)日期之間的月份差數(shù),參數(shù)前者大于后者返回正數(shù),前者小于后者返回負(fù)數(shù)。Expression:months_between(date1,date2);
Example:
select months_between(to_date('2018-8-4','YYYY-MM-DD'),to_date('1949-10-1','YYYY-MM-DD')) from dual;
6、next_day函數(shù):用于返回特定日期后的特定某一天(比如工作日)Expression:next_day(date,str)其中str對(duì)應(yīng)特定某一天,必須與日期語言匹配,如果語言是America,那么周一為Monday,語言是簡(jiǎn)體中文,對(duì)應(yīng)星期一。
Example:
select next_day(sysdate,'星期一') from dual;
7、last_day函數(shù):用于取特定日期的當(dāng)月最后一天。Expression:last_day(date)
Example:
select last_day(sysdate) from dual;
四、轉(zhuǎn)換函數(shù):
1、to_char函數(shù):分為2類
(1)日期轉(zhuǎn)字符:Expression: to_char(date,format,nlsparams)其中nlsparams指定日期的顯示語言,格式為:'NLS_DATE_LANGUAGE=LANGUAGE',也可以不要。
Example:
select to_char(sysdate,'DD-MON-RR','NLS_DATE_LANGUAGE=AMERICAN') from dual;
select to_char(sysdate,'YYYY"年"MM"月"DD"日"') from dual;
注意:如果格式中帶有字符時(shí),須將字符用雙引號(hào)引起來。
(2)數(shù)值轉(zhuǎn)字符:Expression: to_char(number,format)
此處格式常用有:1) 9:顯示數(shù)字,忽略前導(dǎo)0;2)0:顯示數(shù)字,位數(shù)不足用0補(bǔ)齊;3).:小數(shù)點(diǎn); 4),:逗號(hào) 5)$:顯示美元符號(hào); 6)L:本地貨幣符號(hào)
Example:
select to_char(1314520,'L99,999,99.99') from dual;
2、to_date函數(shù):用于將字符串轉(zhuǎn)化為日期類型 Expression: to_date(str,format,nlsparams)其中nlsparams可不要
Example:
select to_date('1999-02-17','YYYY-MM-DD') from dual;
3、to_number函數(shù):用于將含有數(shù)字的字符串轉(zhuǎn)化為數(shù)值類型:Expression: to_number(str,format)
Example:
select to_number('$1314520','$999999999.99') from dual;
注意:此處format的作用限制了數(shù)的范圍,當(dāng)format里面的格式如果比字符串中的數(shù)字小的話,會(huì)報(bào)錯(cuò),示例如下:
select to_number('$1314520','$99999.99') from dual;
五、通用函數(shù):
下面實(shí)例多用到Oracle中scott用戶下的emp表,數(shù)據(jù)如下:
1、nvl函數(shù):解決值為null的問題,當(dāng)?shù)谝粋€(gè)參數(shù)為空時(shí),返回第二個(gè)參數(shù)的值 Expression: nvl(expr1,expr2)
Example:
select nvl(comm,000) from emp;
注意表達(dá)式2中值的類型須和原來字段的類型相同。
2、nvl2函數(shù):如果參數(shù)列表中,第一個(gè)表達(dá)式的值不為空,則返回第二個(gè)表達(dá)式的值,如果為空,則返回第三個(gè)表達(dá)式的值。Expression: nvl2(expr1,expr2,expr3)
Example:
select nvl2(comm,comm,000) from emp;
3、nullif函數(shù):用于判斷2個(gè)表達(dá)式的值是否相等,相等返回null,不等返回第一個(gè)表達(dá)式的值。Expression: nullif(expr1,expr2);
Example:
select nullif(job,'MANAGER') from emp;
4、coalesce函數(shù):用于返回表達(dá)式中第一個(gè)not null的結(jié)果 Expression:coalesce(expr1,expr2,expr2,expr...)
Example:
select coalesce(comm,null,0) from emp;
5、case表達(dá)式:根據(jù)取值不同返回不同的結(jié)果。 Expression:
case expr
when value1 return result1
when value2 return result2
when value3 return result3
when ......
else result
end
Example:
select ename,case jobwhen 'CLERK' then '辦事員'when 'SALESMAN' then '銷售'when 'MANAGER' then '經(jīng)理'when 'ANALYST' then '分析員'else '總裁'endfrom emp
6、decode函數(shù):根據(jù)取值不同返回不同的結(jié)果。和case表達(dá)式的作用相同,Expression: decode(expr,'value1','result1','value2','result2',...,result)
當(dāng)expr的值為value1是返回結(jié)果result1,值為value2時(shí)返回結(jié)果result2,如果都沒有就返回默認(rèn)值result;
Example:
select ename,decode(job,'CLERK','辦事員','SALESMAN','銷售','MANAGER','經(jīng)理','ANALYST','分析員','總裁')from emp;
7、sign函數(shù):判斷當(dāng)前數(shù)字的符號(hào),大于0返回1,等于0返回0,小于0返回-1 Expression: sign(number)
Example:
select sign(13),sign(0),sign(-14) from dual;
8、ascii函數(shù):用于返回當(dāng)前字符對(duì)應(yīng)的ascii碼表中的位置 Expression: ascii(str)
Example:
select ascii('a') from dual;
9、chr函數(shù):和ascii函數(shù)剛好相反,返回ascii碼表中的字符 Expression: chr(number)
Example:
select chr(97) from dual;