Oracle函數-單行函數-字符單行函數


wKiom1hhItLi-gZ4AABh8bZO-Vc870.png-wh_50


函數的分類

=========================================================

單行函數:一個input對應一個output,input和output存在一一對應的關係 如lower

組函數:   多個input,但是隻對應一個output。如 sum()


=========================================================

單行函數

特點:

每行返回一個結果,輸入輸出存在一一對應的關係

能嵌套使用 ,一個函數的輸出能做另外一個函數的輸入 如:select lowner(upper('aa')) from dual;

傳入的變量可以是列的值,也可以是表達式。 如 select lower(ename) from emp;


=========================================================


字符函數:

1、大小寫處理(upper lower),對字符串

SQL>select upper(ename),sal from emp;

SQL>select lower(ename),sal from emp;


2、連接函數 concat

SQL>select concat('Hello','World') from dual;


3、截取 substr(c1,n1,n2) 

n1 開始時候的位置

n2 截取的長度,默認爲截取到最後。

SQL>select substr('HelloWorld',6,2) from dual;



4、instr 指定字符在某個字符串中的第幾位

SQL>select instr('abcdefg','c') from dual;  c在字符串xxx的第幾位

SQL>select instr('abcdefg','c',-1) from dual; 從右邊往左邊數,第一個c在第幾位 第三個參數表示查找的方向

SQL>select instr('abcdefgc','c',-1) from dual;

SQL>select instr('abcdefgc','c',-1,2) from dual;


5、取長 length

select length('HeloWorld') from dual;

select length('') from dual;


6、位數填充

①左填充 lpad(c1,n,c2) 返回指定長度爲n的字符串

SQL>select lpad(sal,10,'*') from emp;

②右填充

SQL>select rpad(sal,10,'*') from emp;  


7、修剪trim

SQL>select trim('    Hello     ') from dual;  --截取,兩邊的空格。作用 留言板空格的問題,可能不小心按住了很多空格

SQL>select ltrim('    Hello     ') from dual;

SQL>select rtrim('    Hello     ') from dual;


8、例子

SQL>select empno,concat('name is ',ename) NAME,job,length(ename),instr(ename,'A') "contains 'A'"

from emp

where substr(job,5) = 'GER';


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