转自:http://www.myexception.cn/operating-system/480929.html
PostgreSQL模仿Oracle的instr函数
- --
- --instrfunctionsthatmimicOracle'scounterpart
- --Syntax:instr(string1,string2,[n],[m])where[]denotesoptionalparameters.
- --
- --Searchesstring1beginningatthenthcharacterforthemthoccurrence
- --ofstring2.Ifnisnegative,searchbackwards.Ifmisnotpassed,
- --assume1(searchstartsatfirstcharacter).
- CREATEFUNCTIONinstr(varchar,varchar)RETURNSintegerAS$$
- DECLARE
- posinteger;
- BEGIN
- pos:=instr($1,$2,1);
- RETURNpos;
- END;
- $$LANGUAGEplpgsqlSTRICTIMMUTABLE;
- FUNCTIONinstr(stringinteger)
- AS$$
- DECLARE
- integerNOTNULLDEFAULT0;
- temp_strvarchar;
- beginteger;
- lengthss_lengthBEGIN
- IFbeg_index>0THEN
- temp_str:=substring(stringFROMbeg_index);
- pos:=position(string_to_searchINtemp_str);
- IFpos=0RETURN0;
- ELSE
- RETURNpos+beg_index-1;
- ENDIF;
- ELSE
- ss_length:=char_length(string_to_search);
- length:=char_length(string);
- beg:=length+beg_index-ss_length+2;
- WHILEbeg>0LOOP
- FROMbegFORss_length);
- IFpos>0RETURNbeg;
- beg:=beg-1;
- ENDLOOP;
- beg_indexinteger,occur_indexinteger)
- DEFAULT0;
- occur_numberiTHEN
- beg:=beg_index;
- FORiIN1..occur_indexLOOP
- IFi=1beg:=beg+pos-1;
- beg:=beg+pos;
- FROMbeg+1);
- RETURN0;
- RETURNbeg;
- ENDIF;
- ss_length:=char_length(string_to_search);
- length:=char_length(string);
- beg:=length+beg_index-ss_length+2;
- WHILEbeg>0LOOP
- FORss_length);
- pos:=position(string_to_searchINtemp_str);
- occur_number:=occur_number+1;
- IFoccur_number=occur_indexbeg:=beg-1;
- ENDLOOP;
- END;
- $$LANGUAGEplpgsqlSTRICTIMMUTABLE;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。