You are now on my old blog. Please update your bookmarks to my new blog
http://laurentschneider.com




30 January 2006

Recursive PL/SQL

it will be a good week !

I found an elegant way to solve a query with recursive pl/sql.

an user wanted to have DHSGHDADSFDF translated in DHSGAF, that is, duplicated chars removed, order retained.

here is my function :

create or replace function f(v varchar2) return varchar2 is
begin
if (v is null) then return null;
else return substr(v,1,1)||f(replace(substr(v,2),substr(v,1,1));
end if;
end;
/


ref: using recursion with PL/SQL

3 Comments:

Anonymous Anonymous said...

SQL> select f('DHSGHDADSFDF') from dual
2 /

F('DHSGHDADSFDF')
----------------------------------------
DHSGHDADSFDF

SQL>

?

30/1/06 16:13  
Anonymous Anonymous said...

Probably is meant
CREATE OR REPLACE FUNCTION f(v VARCHAR2) RETURN VARCHAR2 IS
BEGIN
IF (v IS NULL) THEN
RETURN NULL;
ELSE
RETURN Substr(v, 1, 1) || f(REPLACE(v, Substr(v, 1, 1), ''));
END IF;
END;
/

Regards
Maxim

30/1/06 18:36  
Blogger Laurent Schneider said...

sorry friends, I apologize for this copy paste error... I just posted my first try, but now I corrected my post!

30/1/06 22:51  

Post a Comment

<< Home