Solved

how to write PL/SQL function

Posted on 2008-10-02
3
2,020 Views
Last Modified: 2013-12-07
How to do a pl/sql function that takes two strings representing a list of
numbers separated by commas and returns a string representing the list
of each nth element added together.  You don't know how long the list
will be, but assume it has to fit in a PL/SQL varchar2.

So,

      add_nums('1,2,3,4','3,4,5,6') -> '4,6,8,10'
      add_nums('1,2,3','15,14,13') -> '16,16,16'

It should fail gracefully when the lengths of the two lists are not
equal.
0
Comment
Question by:wasabi3689
3 Comments
 
LVL 32

Accepted Solution

by:
awking00 earned 90 total points
ID: 22625590
See attached.
add-nums.txt
0
 
LVL 27

Expert Comment

by:sujith80
ID: 22626390
A neater version:

create or replace function test_func(p_arg1 varchar2, p_arg2 varchar2)
return varchar2
as
begin
 if ( instr(p_arg1,',') = 0 and instr(p_arg2,',') = 0 ) then
  return to_number(p_arg1) + to_number(p_arg2);
 elsif (instr(p_arg1,',') = 0 OR instr(p_arg2,',') = 0) then
  raise_application_error(-20001, 'Length of the strings are not equal');
 else
  return to_char(to_number(substr(p_arg1, 1, instr(p_arg1,',') - 1)) + to_number(substr(p_arg2, 1, instr(p_arg2,',') - 1)))
         ||','||
         test_func(substr(p_arg1, instr(p_arg1,',') + 1 ), substr(p_arg2, instr(p_arg2,',') + 1 ));
 end if;
end;
/
0
 
LVL 3

Expert Comment

by:gajmp
ID: 22654602
We can achive this in SQL itself. pls check the below SQL. In this we have to provide two string with comma delimeter. Sting should not end with comma. we can give any length of string lk
str = 1,2,3,4 str1=4,5,6,7 op=5,7,9,10,7
str=1,2,3,4,5 str1=2,3,4,5 op=3,5,7,9,5
str=1,2,3,4 str1=3,4,5,6 op=4,6,8,10

select op from (
select r, substr(sys_connect_by_path(tot, ','),2) op
from (
select r, sum(a)+sum(b) tot
from (
select rownum r, to_number(decode(rownum, 1, substr('&&str', 1, instr('&&str',',',1)-1),
                      length(translate('&&str', ',1234567890', ','))+1, substr('&&str',instr('&&str', ',',1,rownum-1)+1,length('&&str')),
                      substr('&&str', instr('&&str', ',',1,rownum-1)+1, (instr('&&str',',',1, rownum)-1 - instr('&&str', ',',1,rownum-1))))) a, 0 b
from all_objects
where rownum <= length(translate('&&str', ',1234567890', ','))+1
union
select rownum r, 0 a, to_number(decode(rownum, 1, substr('&&str1', 1, instr('&&str1',',',1)-1),
                      length(translate('&&str1', ',1234567890', ','))+1, substr('&&str1',instr('&&str1', ',',1,rownum-1)+1,length('&&str1')),
                      substr('&&str1', instr('&&str1', ',',1,rownum-1)+1, (instr('&&str1',',',1, rownum)-1 - instr('&&str1', ',',1,rownum-1))))) b
from all_objects
where rownum <= length(translate('&&str1', ',1234567890', ','))+1)
group by r) x
start with r=1
connect by prior r = r-1
order by r desc)
where rownum < 2
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question