標籤:串連符 sni star snippet sub mat osi 多個 關鍵字
第一:去重 使用distinct關鍵字。
第二:補位函數 LPAD (左補位),RPAD(右補位)
eg: 工資列不足15使用“$”左補足15位( 使用LPAD方法實現對工資列的左補位,SQL語句如下所示)
SELECT ename, LPAD(sal, 15, ‘$‘) as "SALARY" FROM emp ;
第三:字串函數 使用 concat() 或使用 || 串連符 。
eg:select empname || ‘:‘ || sal from emp 使用 || 更為直觀 。
第四:TRIM作用截去字串,經常用來去掉字串前後空格。
第五:SUBSTR()擷取字串的子串 INSTR(sourceString,destString,start,appearPosition) 或 instr(‘源字串‘ , ‘目標字串‘ ,‘開始位置‘,‘第幾次出現‘) 擷取子字串在原字串的位置 start和appearPosition都預設是1 。
用法:substr(str,start,end),或 substr(str,start) 如果start=0則從首字元開始,如果start等於負數則從尾部開始。
instr(‘abcdefgh‘,‘de‘) select instr(‘abcdefgh‘,‘de‘) position from dual; 返回 4 解釋:從1開始算 d排第四所以返回4
select instr(‘abcdefghbc‘,‘bc‘,3) position from dual; 返回 9 解釋:從第3個字元開始算起 第3個字元是c,所以從3開始以後的字串找尋找bc,返回9
select instr(‘qinyinglianqin‘,‘qin‘, 1, 2) position from dual; 返回12 解釋:從第1個字元開始,尋找第2次出現子串的位置
substr()經常和 instr() 一塊使用 通過instr()擷取自古傳的位置 substr() 利用instr返回來的位置 做起始或結束位置
第六:nvl(expr1,expr2) 和 nvl2(expr1,expr2,expr3) 是空值函數
nvl(expr1,expr2) 如果exp1是null 則取值exp2 exp2是實際值 exp1和exp2 可以是任何資料類型,但是exp1和exp2必須是相同的資料類型。
eg:select ename,sal,common sal+nvl(common,o) as salary from emp
nvl2(expr1,expr2,exp3) 判斷如果exp1不是空則返回exp2,如果exp1是空則返回exp3
eg:select ename,sal,common nvl2(commom,sal+commom,sal) as salary from em
第七:分組函數 group by () having() having()必須在group by()後使用
第八:order by 升序降序 (可以按字母、數字、日期) 預設是asc升序 降序必須表明 desc 如果對多個欄位用排序 那麼每個欄位必須表明是升序還是降序
oracle常用函數