PostgreSQL仿照Oracle的instr函数

前端之家收集整理的这篇文章主要介绍了PostgreSQL仿照Oracle的instr函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://www.myexception.cn/operating-system/480929.html


Postgresql模仿Oracle的instr函数

  1. --
  2. --instrfunctionsthatmimicOracle'scounterpart
  3. --Syntax:instr(string1,string2,[n],[m])where[]denotesoptionalparameters.
  4. --
  5. --Searchesstring1beginningatthenthcharacterforthemthoccurrence
  6. --ofstring2.Ifnisnegative,searchbackwards.Ifmisnotpassed,
  7. --assume1(searchstartsatfirstcharacter).
  8. CREATEFUNCTIONinstr(varchar,varchar)RETURNSintegerAS$$
  9. DECLARE
  10. posinteger;
  11. BEGIN
  12. pos:=instr($1,$2,1);
  13. RETURNpos;
  14. END;
  15. $$LANGUAGEplpgsqlSTRICTIMMUTABLE;
  16. FUNCTIONinstr(stringinteger)
  17. AS$$
  18. DECLARE
  19. integerNOTNULLDEFAULT0;
  20. temp_strvarchar;
  21. beginteger;
  22. lengthss_lengthBEGIN
  23. IFbeg_index>0THEN
  24. temp_str:=substring(stringFROMbeg_index);
  25. pos:=position(string_to_searchINtemp_str);
  26. IFpos=0RETURN0;
  27. ELSE
  28. RETURNpos+beg_index-1;
  29. ENDIF;
  30. ELSE
  31. ss_length:=char_length(string_to_search);
  32. length:=char_length(string);
  33. beg:=length+beg_index-ss_length+2;
  34. WHILEbeg>0LOOP
  35. FROMbegFORss_length);
  36. IFpos>0RETURNbeg;
  37. beg:=beg-1;
  38. ENDLOOP;
  39. beg_indexinteger,occur_indexinteger)
  40. DEFAULT0;
  41. occur_numberiTHEN
  42. beg:=beg_index;
  43. FORiIN1..occur_indexLOOP
  44. IFi=1beg:=beg+pos-1;
  45. beg:=beg+pos;
  46. FROMbeg+1);
  47. RETURN0;
  48. RETURNbeg;
  49. ENDIF;
  50. ss_length:=char_length(string_to_search);
  51. length:=char_length(string);
  52. beg:=length+beg_index-ss_length+2;
  53. WHILEbeg>0LOOP
  54. FORss_length);
  55. pos:=position(string_to_searchINtemp_str);
  56. occur_number:=occur_number+1;
  57. IFoccur_number=occur_indexbeg:=beg-1;
  58. ENDLOOP;
  59. END;
  60. $$LANGUAGEplpgsqlSTRICTIMMUTABLE;
原文链接:https://www.f2er.com/postgresql/193143.html

猜你在找的Postgre SQL相关文章