最近有个需求,要求要剔除同一数字或字母连续出现次数大于等于3次的数值。问了很多朋友和同事,一开始说要用正则写,但是实在是不会写正则,然后一个朋友就给了一个思路。是一个比较笨的方法,但也姑且算是一种解决思路吧。
先建一个中间表,将连续的字母和数字insert到该表中,如’AAA’、‘111’ 等等
create table p_str_tmp(str character varying);
insert into p_str_tmp(str)
select AAA’ str union all
select BBB’ str union all
select CCC’ str union all
select DDD’ str union all
select EEE’ str union all
select FFF’ str union all
select GGG’ str union all
select HHH’ str union all
select III’ str union all
select JJJ’ str union all
select KKK’ str union all
select LLL’ str union all
select MMM’ str union all
select NNN’ str union all
select OOO’ str union all
select PPP’ str union all
select QQQ’ str union all
select RRR’ str union all
select SSS’ str union all
select TTT’ str union all
select UUU’ str union all
select VVV’ str union all
select WWW’ str union all
select XXX’ str union all
select YYY’ str union all
select ZZZ’ str union all
select 000’ str union all
select 111’ str union all
select 222’ str union all
select 333’ str union all
select 444’ str union all
select 555’ str union all
select 666’ str union all
select 777’ str union all
select 888’ str union all
select 999’ str;
然后写函数
create or replace function fun_instr_cnt(v_in_str character varying)
returns numeric AS
B
O
D
Y
BODY
BODY
/*
过程名:fun_instr_cnt
功能描述:判断字符串中是否存在同一数字或字母连续出现
输入参数:v_in_str:字符串
*/
declare
v_cnt_value numeric ;
v_local numeric ;
begin
select max(position(b.str in v_in_str)) into v_local
from p_str_tmp b;
----判断:v_local >0 时,则存在连续数字或字母;否则,则不存在
if v_local > 0
then v_cnt_value :=0;
else
v_cnt_value :=1;
end if;
return v_cnt_value;
end
B
O
D
Y
BODY
BODY
language plpgsql volatile;
将来如果遇到需求更改,可以直接修改表里的数据。比如说,要求要剔除同一数字或字母连续出现4次及以上的字符串,则表中的数据更改为’AAAA’、'0000’等等,这样。
PS:第一次写csdn博客,感觉还不太会用。。。(函数中BODY单词的前后都有 $符号,不知道为什么显示不出来)