Solved

how to write PL/SQL function

Posted on 2008-10-02
3
2,000 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 31

Accepted Solution

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

Expert Comment

by:sujith80
Comment Utility
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
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql to get orphans 7 50
CREATE TABLE syntax 4 44
update set column values in oracle 3 43
SQL Retrieve Values 4 40
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now