Oracle中使用到的函式

2021-10-05 13:30:45 字數 3485 閱讀 7102

1.字串相關

(1)查詢字串

insrt函式 對指定字串進行判斷,判斷其是否含有指定的字元

instr('源字串' , '目標字串' ,'開始位置','第幾次出現')

用於模糊查詢以及判斷包含關係

select code,name,dept,occupation from staff where instr(code, '001')> 0;

等同於:where code like '%001%' ;

例如:select * from info where instr( '11',id)>0;

(2)字串拼接

①使用「||」來拼接字串:

select '拼接'||'字串' as str from student;

②使用concat(param1,param2)函式實現:

select concat('拼接','字串') as str from student;

注:oracle的concat()方法只支援兩個引數,如果拼接多個引數,可以巢狀concat():

select concat(concat('拼接','字串'),'ab') as str from student;

(3)字串擷取

substr(string,start_position,[length]),獲取子字串

substr('源字串',開始位置,字串的長度[可選])

例如:substr("abcdefg", 0); //返回:abcdefg,擷取所有字元

substr("abcdefg", 2); //返回:cdefg,擷取從c開始之後所有字元

substr("abcdefg", 0, 3); //返回:abc,擷取從a開始3個字元

substr("abcdefg", 0, 100); //返回:abcdefg,100雖然超出預處理的字串最長度,但不會影響返回結果,系統按預處理字串最大數量返回。

substr("abcdefg", -3); //返回:efg,注意引數-3,為負值時表示從尾部開始算起,字串排列位置不變。

(4)替換字串

replace(strsource, str1, str2) 將strsource中的str1替換成str2

replace('源字串', '要替換的字串', '替換後的字串')

select '替換字串' as oldstr, replace('替換字串', '替換', '修改') as newstr from dual

2.decode函式 用法 列舉值值轉換,用於比較大小

解釋:decode(條件,值1,返回值1,值2,返回值2,...值n,返回值n,預設值)

例如:select

decode( t.audit_type, '01', f1.data_value, '02', f2.data_value, '03', f3.data_value ) catalogsortname,

t.*from

directory t

left join data f1 on f1.catalog_code = 'da12'

and t.catalog_sort = f1.data_code

and t.audit_type = '01'

left join data f2 on f2.catalog_code = 'hzml'

and t.catalog_sort = f2.data_code

and t.audit_type = '02'

left join data f3 on f3.catalog_code = 'baml'

and t.catalog_sort = f3.data_code

and t.audit_type = '03'

3.sys_guid()  生成uuid

亂碼時解決:rawtohex(sys_guid())

4.insert into tab1 (id,name,age) select (rawtohex(sys_guid()),name,age) from tab2  

可以將查詢的資料一次批量新增,提高效率

5.exists、not exists 、in、not in 的用法

exists (sql 返回結果集為真)

not exists (sql 不返回結果集為真)

in,not in,exists,not exists的用法和差別:

in:確定給定的值是否與子查詢或列表中的值相匹配。

not in:通過 not in 關鍵字引入的子查詢也返回一列零值或更多值。

使用 exists 和 not exists 引入的子查詢可用於兩種集合原理的操作:交集與差集。

兩個集合的交集包含同時屬於兩個原集合的所有元素。

差集包含只屬於兩個集合中的第乙個集合的元素。

exists:指定乙個子查詢,檢測行的存在。

兩者的區別:

exists:後面可以是整句的查詢語句如:select * from titles

in:後面只能是對單列:select pub_id from titles

not exists比not in速度快(在not exists子查詢裡的語句比較複雜時,有可能比not in慢), 最好將not in 轉化成not exists。另外,要注意的是,in或者not in裡面都不能有null值,否則會得不到結果。

如果資料量很小的話,in和exists的差距是忽略不計的,但資料量很大時差距就很大了!in會在先執行一次後面的大表中的全表掃瞄,然後生成乙個新的虛表再進行全表掃瞄,而exists只執行一次後面的大表的掃瞄,每次掃瞄為真時就返回,繼續往下走!所以在資料量很大時這個效率的差距也是驚人的!

6.listagg() within group ()

使用  listagg() within group ()  將多行合併成一行(比較常用)

使用 listagg() within group () over  將多行記錄在一行顯示

7.求多列的最大值,oracle中的greatest 函式8.

--統計某月資料個數

select to_char(exchange_time,'yyyy-mm') as months,count(distinct uuid) as count from  table    where 1=1

and exchange_time <=to_date('2019-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss')

group by to_char(exchange_time,'yyyy-mm');

Oracle中使用Split函式

定義陣列型別 type t array is varray 2000 of varchar2 4000 v varray t array 將字串轉換成為陣列 陣列解析函式摘的,不應該每個字元遍歷,只作啟示 function f splitstr str in varchar2,separator i...

程序通訊中使用到的函式講解

函式可用於建立乙個管道,以實現程序間的通訊。pipe函式的定義如下 includeint pipe int fd 2 pipe函式定義中的fd引數是乙個大小為2的乙個陣列型別的指標。該函式成功時返回0,並將一對開啟的檔案描述符值填入fd引數指向的陣列。失敗時返回 1並設定errno。通過pipe函式...

Oracle使用到的一些函式

2.trim 函式 去除字串兩端的空格 3.length 函式 統計字串的長度 4.case when else end as 5.decode函式 decode value,if1,then1,if2,then2,if3,then3,else 6.substr 函式 substr string s...