将函数从 Oracle 转换为 PostgreSQL
Converting function from Oracle to PostgreSQL
我正在努力将某些东西从 Oracle 转换为 PostgreSQL。在Oracle文件中有一个函数:
instr(string,substring,starting point,nth location)
或在我的文件中
instr(string,chr(10),instr(string,substring),1)
在 PostgreSQL 中这不存在,所以我查找了一个等效函数。我发现:
position(substring in string)
但这不允许起始位置和第n个位置参数。
有没有办法让这个函数从给定的点开始?或者在 PostgreSQL 中是否有更好的函数可以指定起始位置和第 n 个位置?
这必须在 PostgreSQL 8.2.15 上工作,因为这是我们在数据库上 运行 的版本。
最简单的形式:
instr(string, substring) ::= strpos(string, substring)
带有一个position
参数:
对于正 position
值:
instr(string, substring, position) ::= strpos(substr(string, position), substring) + position - 1
对于负 position
值:
instr(string, substring, position) ::= strpos(substr(string, char_length(string) + position + 1), substring) + char_length(string) + position
使用 occurrence
参数:
这在 PostgreSQL 中不存在。您似乎不需要它(示例给出 occurrence = 1
),但如果需要,则需要编写一个函数,该函数递归地处理从第二个版本中提取的子字符串。
所以:
instr(string,chr(10),instr(string,substring),1)
变成
strpos(substr(string, strpos(string, substring)), chr(10)) + strpos(string, substring) - 1
Postgres 中的函数strpos(str, sub)
等同于Oracle 中的instr(str, sub)
。不幸的是,该函数没有第三和第四个参数,因此Postgres中的表达式必须更复杂。
函数substr(str, n)
从n
位置开始给出str
的子串。
instr(str, ch, instr(str, sub), 1); --oracle
strpos(substr(str, strpos(str, sub)), ch) + strpos(str, sub) - 1; --postgres
由于instr()
是一个强大的功能,我根据自己的需要在plpgsql中编写了它。
create or replace function instr(str text, sub text, startpos int = 1, occurrence int = 1)
returns int language plpgsql immutable
as $$
declare
tail text;
shift int;
pos int;
i int;
begin
shift:= 0;
if startpos = 0 or occurrence <= 0 then
return 0;
end if;
if startpos < 0 then
str:= reverse(str);
sub:= reverse(sub);
pos:= -startpos;
else
pos:= startpos;
end if;
for i in 1..occurrence loop
shift:= shift+ pos;
tail:= substr(str, shift);
pos:= strpos(tail, sub);
if pos = 0 then
return 0;
end if;
end loop;
if startpos > 0 then
return pos+ shift- 1;
else
return length(str)- length(sub)- pos- shift+ 3;
end if;
end $$;
一些检查(示例来自 OLAP DML Functions):
select instr('Corporate Floor', 'or', 3, 2); -- gives 14
select instr('Corporate Floor', 'or', -3, 2); -- gives 2
Postgres 8.2 中没有 reverse()
函数。你可以使用这个:
-- only for Postgres 8.4 or earlier!
create or replace function reverse(str text)
returns text language plpgsql immutable
as $$
declare
i int;
res text = '';
begin
for i in 1..length(str) loop
res:= substr(str, i, 1) || res;
end loop;
return res;
end $$;
你可以在postgres中使用split_part()函数
参考以下link
https://www.postgresqltutorial.com/postgresql-split_part/
SELECT SPLIT_PART('A,B,C', ',', 2);
我正在努力将某些东西从 Oracle 转换为 PostgreSQL。在Oracle文件中有一个函数:
instr(string,substring,starting point,nth location)
或在我的文件中
instr(string,chr(10),instr(string,substring),1)
在 PostgreSQL 中这不存在,所以我查找了一个等效函数。我发现:
position(substring in string)
但这不允许起始位置和第n个位置参数。
有没有办法让这个函数从给定的点开始?或者在 PostgreSQL 中是否有更好的函数可以指定起始位置和第 n 个位置?
这必须在 PostgreSQL 8.2.15 上工作,因为这是我们在数据库上 运行 的版本。
最简单的形式:
instr(string, substring) ::= strpos(string, substring)
带有一个position
参数:
对于正 position
值:
instr(string, substring, position) ::= strpos(substr(string, position), substring) + position - 1
对于负 position
值:
instr(string, substring, position) ::= strpos(substr(string, char_length(string) + position + 1), substring) + char_length(string) + position
使用 occurrence
参数:
这在 PostgreSQL 中不存在。您似乎不需要它(示例给出 occurrence = 1
),但如果需要,则需要编写一个函数,该函数递归地处理从第二个版本中提取的子字符串。
所以:
instr(string,chr(10),instr(string,substring),1)
变成
strpos(substr(string, strpos(string, substring)), chr(10)) + strpos(string, substring) - 1
Postgres 中的函数strpos(str, sub)
等同于Oracle 中的instr(str, sub)
。不幸的是,该函数没有第三和第四个参数,因此Postgres中的表达式必须更复杂。
函数substr(str, n)
从n
位置开始给出str
的子串。
instr(str, ch, instr(str, sub), 1); --oracle
strpos(substr(str, strpos(str, sub)), ch) + strpos(str, sub) - 1; --postgres
由于instr()
是一个强大的功能,我根据自己的需要在plpgsql中编写了它。
create or replace function instr(str text, sub text, startpos int = 1, occurrence int = 1)
returns int language plpgsql immutable
as $$
declare
tail text;
shift int;
pos int;
i int;
begin
shift:= 0;
if startpos = 0 or occurrence <= 0 then
return 0;
end if;
if startpos < 0 then
str:= reverse(str);
sub:= reverse(sub);
pos:= -startpos;
else
pos:= startpos;
end if;
for i in 1..occurrence loop
shift:= shift+ pos;
tail:= substr(str, shift);
pos:= strpos(tail, sub);
if pos = 0 then
return 0;
end if;
end loop;
if startpos > 0 then
return pos+ shift- 1;
else
return length(str)- length(sub)- pos- shift+ 3;
end if;
end $$;
一些检查(示例来自 OLAP DML Functions):
select instr('Corporate Floor', 'or', 3, 2); -- gives 14
select instr('Corporate Floor', 'or', -3, 2); -- gives 2
Postgres 8.2 中没有 reverse()
函数。你可以使用这个:
-- only for Postgres 8.4 or earlier!
create or replace function reverse(str text)
returns text language plpgsql immutable
as $$
declare
i int;
res text = '';
begin
for i in 1..length(str) loop
res:= substr(str, i, 1) || res;
end loop;
return res;
end $$;
你可以在postgres中使用split_part()函数
参考以下link
https://www.postgresqltutorial.com/postgresql-split_part/
SELECT SPLIT_PART('A,B,C', ',', 2);