PostgreSQL模仿Oracle的instr函数
2018-06-17 19:51:26来源:未知 阅读 ()
- --
- -- instr functions that mimic Oracle's counterpart
- -- Syntax: instr(string1, string2, [n], [m]) where [] denotes optional parameters.
- --
- -- Searches string1 beginning at the nth character for the mth occurrence
- -- of string2. If n is negative, search backwards. If m is not passed,
- -- assume 1 (search starts at first character).
- --
- CREATE FUNCTION instr(varchar, varchar) RETURNS integer AS $$
- DECLARE
- pos integer;
- BEGIN
- pos:= instr($1, $2, 1);
- RETURN pos;
- END;
- $$ LANGUAGE plpgsql STRICT IMMUTABLE;
- CREATE FUNCTION instr(string varchar, string_to_search varchar, beg_index integer)
- RETURNS integer AS $$
- DECLARE
- pos integer NOT NULL DEFAULT 0;
- temp_str varchar;
- beg integer;
- length integer;
- ss_length integer;
- BEGIN
- IF beg_index > 0 THEN
- temp_str := substring(string FROM beg_index);
- pos := position(string_to_search IN temp_str);
- IF pos = 0 THEN
- RETURN 0;
- ELSE
- RETURN pos + beg_index - 1;
- END IF;
- ELSE
- ss_length := char_length(string_to_search);
- length := char_length(string);
- beg := length + beg_index - ss_length + 2;
- WHILE beg > 0 LOOP
- temp_str := substring(string FROM beg FOR ss_length);
- pos := position(string_to_search IN temp_str);
- IF pos > 0 THEN
- RETURN beg;
- END IF;
- beg := beg - 1;
- END LOOP;
- RETURN 0;
- END IF;
- END;
- $$ LANGUAGE plpgsql STRICT IMMUTABLE;
- CREATE FUNCTION instr(string varchar, string_to_search varchar,
- beg_index integer, occur_index integer)
- RETURNS integer AS $$
- DECLARE
- pos integer NOT NULL DEFAULT 0;
- occur_number integer NOT NULL DEFAULT 0;
- temp_str varchar;
- beg integer;
- i integer;
- length integer;
- ss_length integer;
- BEGIN
- IF beg_index > 0 THEN
- beg := beg_index;
- temp_str := substring(string FROM beg_index);
- FOR i IN 1..occur_index LOOP
- pos := position(string_to_search IN temp_str);
- IF i = 1 THEN
- beg := beg + pos - 1;
- ELSE
- beg := beg + pos;
- END IF;
- temp_str := substring(string FROM beg + 1);
- END LOOP;
- IF pos = 0 THEN
- RETURN 0;
- ELSE
- RETURN beg;
- END IF;
- ELSE
- ss_length := char_length(string_to_search);
- length := char_length(string);
- beg := length + beg_index - ss_length + 2;
- WHILE beg > 0 LOOP
- temp_str := substring(string FROM beg FOR ss_length);
- pos := position(string_to_search IN temp_str);
- IF pos > 0 THEN
- occur_number := occur_number + 1;
- IF occur_number = occur_index THEN
- RETURN beg;
- END IF;
- END IF;
- beg := beg - 1;
- END LOOP;
- RETURN 0;
- END IF;
- END;
- $$ LANGUAGE plpgsql STRICT IMMUTABLE;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 60K!刚面完Python!这个被Oracle裁掉的程序员求职刷爆全网 2019-06-14
- Django的admin源码浅析和模仿 2019-05-08
- Python练手例子(8) 2019-02-17
- Python连接三大数据库MS Sql Server、Oracle、MySQL 2018-11-28
- python遍历文件 2018-10-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash