oracle內置函數(2)字符型

chr(n):把指定的Ascii碼轉換爲字符。

select chr(97) from dual; 輸出a。

ascii(char):返回指定char的ascii碼。

select ascii('a') from dual; 輸出97。

length(string):返回指定字符串的長度。

select length('helloworld') from dual; 輸出10。

upper(char):將字符串轉換爲大寫字母。

select upper('helloworld') from dual;輸出HELLOWORLD。

lower(char):將字符串轉換爲小寫。

select lower('HELLOWORLD') from dual;輸出helloworld。

initcap(char):將所有單詞的首字母轉換爲大寫。

select initcap('hello world') from dual;輸出Hello World。

instr(string,substring,position,occurrence):搜索string中是否存在substring。position表示搜索的開始位置,默認爲1,如果爲負數,則表示從字符串右邊開始搜索。occurrence表示substring第幾次出現。

select instrI('helloworld','lo') from dual;輸出4。

select instr('helloworld','l',5) from dual;輸出9.

select instr('helloworld','l',1,3) from dual;輸出9.

select instr('helloworld','l',4,3) from dual;輸出0。因爲從第四個字符開始搜索,此時只能搜索到兩個'l',不存在第三個'l',故會返回0.

substr(string,position,sub_length):從string的第position位開始,截取sub_length長度。默認position爲1,如果position爲負數,則從stirng右邊開始截取。

select substr('helloworld',4,2) from dual;輸出lo。

select substr('helloworld',4,2) from dual;輸出or。

trim([leading/trailing/both][trim_character FROM]trim_source):刪除指定的前綴或尾隨字符。默認刪除空格。

leading表示刪除前綴字符。trailing表示刪除後綴字符。both表示刪除前綴和後綴字符。截取集只能有一個字符

select trim(leading 'h' from 'helloworld') from dual;輸出elloworld.

select trim(trailing 'd' from 'helloworld') from dual;輸出helloworl.

select trim(both 'h' from 'helloworldh') from dual;輸出elloworld。這裏不能使用(both 'h','d')或(both 'hd')的寫法。

rtrim(char,[set]):將char右邊出現在set的字符刪掉。當遇到非set中的字符時就停止。

select rtrim('helloworld','dl') from dual;輸出hellowor。

select rtirm('helloworld','dr') from dual;輸出helloworl(遇到‘l’時,就停止了刪除)。

ltrim(char,[set]):與rtrim相似,ltrim刪掉char左邊出現在set的字符。

replace(char,string,replace_stirng):將char中的string替換爲replace_tring。

select replace('helloworld','hello','hi') from dual;輸出hiworld。

rpad(string,n,[char]):用char在string的右邊將string的長度增加到n。如果string的長度大於n,則變爲截取string到n的長度。

select rpad('helloworld',11,'1') from dual; 輸出helloworld1。

select rpad('helloworld',13,'1234') from dual;輸出helloworld123。helloworld原長度爲10,用‘1234’中的每個字符去填充helloworld,當string長度爲13時,string變爲helloworld123.此時停止填充。

select rpad('helloworld',8,'1') from dual;輸出hellowor。

發佈了32 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章