時間:2024-02-03 17:42作者:下載吧人氣:21
postgresql中position函數(shù)提供從頭查找返回第一個匹配到字符串的下標(biāo)。
而我需要返回從后向前查找第一個匹配到的坐標(biāo),但是postgressql并未提供相關(guān)函數(shù),所以自己寫了如下代碼提供相關(guān)功能:
CREATE OR REPLACE FUNCTION lastindexof(text, character)
RETURNS integer AS
$BODY$
begin
if $1 is null then return NULL;
end if;
for i in reverse length($1) .. 1
loop
if substr($1,i,1) = $2
then
return i;
end if;
end loop;
return NULL;
end
$BODY$
LANGUAGE plpgsql IMMUTABLE STRICT
網(wǎng)友評論