How to get a function to return a value occuring after a character
I need to write a function to return the next value occurring after the ":" character in a column. If multiple values of ":" occurs then the function should return the sum of the next value occurring after each ":" in the column.
For example a rating value of 4:1 would return the value of 1. However, the rating of "5:1:1" should return a value of 1+1 = 2, and 6:2:1 will return of 2+1 = 3.
I have the below function skeletion and trying to figure out how the select statement will compute based on the position of : in the column and add the values and return the value back to function.
Function fn_check_internalrating(p_internalrating IN VARCHAR2)
RETURN number
IS
cnumber number;
cursor c1 is
select ................................
BEGIN
open c1;
fetch c1 into cnumber;
close c1;
RETURN cnumber;
EXCEPTION
WHEN OTHERS
THEN RETURN NULL;
END;
Hi,
You don't need a cursor: there's no table involved in this function, and no point in using any table.
Here's one way:
CREATE OR REPLACE FUNCTION fn_check_internalrating
( p_internalrating IN VARCHAR2
, p_delimiter IN VARCHAR2 DEFAULT ':'
RETURN NUMBER
DETERMINISTIC -- Same input always produces same output
IS
cnumber NUMBER := 0; -- value to be returned
pos PLS_INTEGER := INSTR ( p_internalrating
, p_delimiter
); -- position where delimiter was found
BEGIN
WHILE pos != 0
LOOP
cnumber := cnumber + TO_NUMBER ( SUBSTR ( p_internalrating
, pos + 1
, 1
pos := INSTR ( p_internalrating
, p_delimiter
, pos + 1
END LOOP;
RETURN cnumber;
END fn_check_internalrating;
SHOW ERRORSThis assumes the function is a stand-alone function. If it's part of a package, you don't say CREATE OR REPLACE at the beginning.
Try to make functions generic, so that if a similar (but not identical) situation comes up in 6 months from now, you can use the same function. I'm guessing that somethimes you may want to do the same thing with some character other than ':' before each number, so I added the 2nd (optional) argument p_delimiter. You can call the fucntion with either 1 or 2 arguments.
If an error occurs in a PL/SQL fucntion, an error message (showing the exact location of the error) is displayed, and execution halts. If you use an EXCEPTION sectinn, you lose all that functionality, or have to code it yourself. Only use an EXCEPTION handler when you really have to.
For this function, you may or may not want to. For example, if the character right after a delimiter is not a digit, the call to TO_NUMBER in function will raise "ORA-01722: invalid number". You may want to catch that error in an exception handler, and return 0 or NULL. On the other hand, you may want to test that the character after the delimiter is a digit before calling TO_NUMBER, and not have an EXCEPTION section.
What else could go wrong? Try to think of potential problems and fix them when you first write the function. If you discover an error next year, you'll have to spend a fair amount of time finding the function, and getting acquainted with it again.
What should the function return if p_internalrating is NULL, or doesn't contain any delimiters?
What if there's a number longer than 1 digit after a delimiter, e.g. '6:78:9'?
Similar Messages
-
How to get number of rows return in SELECT query
i'm very new in java, i have a question:
- How to get number of rows return in SELECT query?
(i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
Thanks.make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
String sql = "select * from testing";
PreparedStatement ps =
con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
rs.last();
System.out.println("Row count = " + rs.getRow());
rs.beforeFirst();~Tim
NOTE: Ugly, but does the trick. -
How to get the function name/ID of the current page
HI,
I searched the forum and google, but it is not clear on how to get the function name of the current page.
I have 4 functions in my self service A ,B,C,D (seeded page links ) they all have to go to a page called hhhPG. Based on which function user is clicks I have to show the data in hhhPG.
so my question is how to get the function name ? I mean I need to know if user click on link A , B, C or D link and came to this page.
I found below in one forum and also there is getfunctionID in pageContext , but I do not know what to pass to getfunctionid("???") to get value.
FunctionSecurity funcSecurity = pageContext.getFunctionSecurity();
Or is there a better way of know where user is coming from ? like geturl....
Please help, I need this ASAP.
Thank you.Use below to find out how the user came into the page
FunctionSecurity funcSecurity = pageContext.getFunctionSecurity();
boolean isFunc1 = funcSecurity.testFunction(funcSecurity.getFunction("YOUR_FUNCTION_SHORT_NAME1"));
boolean isFunc2 = funcSecurity.testFunction(funcSecurity.getFunction("YOUR_FUNCTION_SHORT_NAME2"));
boolean isFunc3 = funcSecurity.testFunction(funcSecurity.getFunction("YOUR_FUNCTION_SHORT_NAME3"));
boolean isFunc4 = funcSecurity.testFunction(funcSecurity.getFunction("YOUR_FUNCTION_SHORT_NAME4"));
So one of them will be true based on the function the user came in.
Write your logic based on these flags.
Regards,
Peddi. -
How to get the function name in controller class
Hi experts ,
I am new to the OAF framework.
i have created the two functions and bot he the function have the same controller class .i want to capture the function name or function id in the controller class.
can you please let me know how to get the function id or function name in the controller class.Hi apurba,
Thanks for the quick reply.
i am trying to get the function name from the FunctionSecurity class,
However in FunctionSecurity class there is no such method defined as getFunctionName();
my requirement is ,i have two functions functionA and functionB defined.
both the function has the same controller class.in controller class ,i need to get the function name ,based on the function name
i will redirect the page to respective page.
looking forward for you response.
appreciate your help
Thanks,
KT -
How to i get my screen to return to normal size after accidentally zooming it in
How do I get my screen to return to normal size after accidentally zooming in
Pinch it, or double tap.
-
XMLTABLE function not returning any values if xml has attribute "xmlns"
Hi,
XMLTABLE function not returning any values if xml has attribute "xmlns". Is there way to get the values if xml has attribute as "xmlns".
create table xmltest (id number(2), xml xmltype);
insert into xmltest values(1,
'<?xml version="1.0"?>
<emps>
<emp empno="1" deptno="10" ename="John" salary="21000"/>
<emp empno="2" deptno="10" ename="Jack" salary="310000"/>
<emp empno="3" deptno="20" ename="Jill" salary="100001"/>
</emps>');
insert into xmltest values(2,
'<?xml version="1.0"?>
<emps xmlns="http://emp.com">
<emp empno="1" deptno="10" ename="John" salary="21000"/>
<emp empno="2" deptno="10" ename="Jack" salary="310000"/>
<emp empno="3" deptno="20" ename="Jill" salary="100001"/>
</emps>');
commit;
SELECT a.*
FROM xmltest,
XMLTABLE (
'for $i in /emps/emp
return $i'
PASSING xml
COLUMNS empno NUMBER (2) PATH '@empno',
deptno NUMBER (3) PATH '@deptno',
ename VARCHAR2 (10) PATH '@ename',
salary NUMBER (10) PATH '@salary') a
WHERE id = 1;
The above query returning results but below query is not returning any results because of xmlns attribute.
SELECT a.*
FROM xmltest,
XMLTABLE (
'for $i in /emps/emp
return $i'
PASSING xml
COLUMNS empno NUMBER (2) PATH '@empno',
deptno NUMBER (3) PATH '@deptno',
ename VARCHAR2 (10) PATH '@ename',
salary NUMBER (10) PATH '@salary') a
WHERE id = 1;
how to get rid out of this problem.
Thanks,
-ManiAdded below one in xmltable, its working now.
XmlNamespaces(DEFAULT 'http://emp.com') -
Function which returns multiple values that can then be used in an SQL Sele
I'd like to create a function which returns multiple values that can then be used in an SQL Select statement's IN( ) clause
Currently, the select statement is like (well, this is a very simplified version):
select application, clientid
from tbl_apps, tbl_status
where tbl_apps.statusid = tbl_status.statusid
and tbl_status.approved > 0;
I'd like to pull the checking of the tbl_status into a PL/SQL function so my select would look something like :
select application, clientid
from tbl_apps
where tbl_apps.statusid in (myfunction);
So my function would be running this sql:
select statusid from tbl_status where approved > 0;
... will return values 1, 5, 15, 32 (and more)
... but I haven't been able to figure out how to return the results so they can be used in SQL.
Thanks for any help you can give me!!
Trisha GorrPerhaps take a look at pipelined functions:
Single column example:
SQL> CREATE OR REPLACE TYPE split_tbl IS TABLE OF VARCHAR2(32767);
2 /
Type created.
SQL> CREATE OR REPLACE FUNCTION split (p_list VARCHAR2, p_delim VARCHAR2:=' ') RETURN SPLIT_TBL PIPELINED IS
2 l_idx PLS_INTEGER;
3 l_list VARCHAR2(32767) := p_list;
4 l_value VARCHAR2(32767);
5 BEGIN
6 LOOP
7 l_idx := INSTR(l_list, p_delim);
8 IF l_idx > 0 THEN
9 PIPE ROW(SUBSTR(l_list, 1, l_idx-1));
10 l_list := SUBSTR(l_list, l_idx+LENGTH(p_delim));
11 ELSE
12 PIPE ROW(l_list);
13 EXIT;
14 END IF;
15 END LOOP;
16 RETURN;
17 END SPLIT;
18 /
Function created.
SQL> SELECT column_value
2 FROM TABLE(split('FRED,JIM,BOB,TED,MARK',','));
COLUMN_VALUE
FRED
JIM
BOB
TED
MARK
SQL> create table mytable (val VARCHAR2(20));
Table created.
SQL> insert into mytable
2 select column_value
3 from TABLE(split('FRED,JIM,BOB,TED,MARK',','));
5 rows created.
SQL> select * from mytable;
VAL
FRED
JIM
BOB
TED
MARK
SQL>Multiple column example:
SQL> CREATE OR REPLACE TYPE myrec AS OBJECT
2 ( col1 VARCHAR2(10),
3 col2 VARCHAR2(10)
4 )
5 /
Type created.
SQL>
SQL> CREATE OR REPLACE TYPE myrectable AS TABLE OF myrec
2 /
Type created.
SQL>
SQL> CREATE OR REPLACE FUNCTION pipedata(p_str IN VARCHAR2) RETURN myrectable PIPELINED IS
2 v_str VARCHAR2(4000) := REPLACE(REPLACE(p_str, '('),')');
3 v_obj myrec := myrec(NULL,NULL);
4 BEGIN
5 LOOP
6 EXIT WHEN v_str IS NULL;
7 v_obj.col1 := SUBSTR(v_str,1,INSTR(v_str,',')-1);
8 v_str := SUBSTR(v_str,INSTR(v_str,',')+1);
9 IF INSTR(v_str,',')>0 THEN
10 v_obj.col2 := SUBSTR(v_str,1,INSTR(v_str,',')-1);
11 v_str := SUBSTR(v_str,INSTR(v_str,',')+1);
12 ELSE
13 v_obj.col2 := v_str;
14 v_str := NULL;
15 END IF;
16 PIPE ROW (v_obj);
17 END LOOP;
18 RETURN;
19 END;
20 /
Function created.
SQL>
SQL> create table mytab (col1 varchar2(10), col2 varchar2(10));
Table created.
SQL>
SQL> insert into mytab (col1, col2) select col1, col2 from table(pipedata('(1,2),(2,3),(4,5)'));
3 rows created.
SQL>
SQL> select * from mytab;
COL1 COL2
1 2
2 3
4 5 -
How to get fourthly row (row4) first column value (col1) in matrix
Hi to all,
In FMS, how to get fourthly row (row4) first column value (col1) in matrix in document.
select $[$38.1.4]
But it display the first row
Please give me hint.
Thank youHi Eric,
FMS may only apply to current row. There is no way to get any other fixed row.
Thanks,
Gordon -
How to get a unique row in a value set
How to get a unique row in a value set which is used in concurrent program.
Example if a table contains 10 unique rows i need only one row to show.
Thanksadd conditions in where clause to supress the duplicate values.
On how to supress the duplicate values follow the link
http://oracleschools.com/index.php?topic=40.msg76#msg76
Thanks
Prudhvi
www.erpschools.com -
How to get the query result of improvement (Before and After ) using sql de
how to get the query result of improvement (Before and After ) using sql developer.
Check
http://www.oracle.com/technetwork/articles/sql/exploring-sql-developer-1637307.html -
How to get tools and other bars back in view after deleting show-bar?
How to get tools and other bars back in view after deleting show-bar?
Hello Jouko.
You can go into View > Toolbars and select the ones you want to see. If you can't see the menu bar you can simply tab ALT on your keyboard (I think F10 works too) to show it. -
How to get or set default fiscal year value in billing doc
Hi All, my question is simple, how to get or set default fiscal year value (VBRK_GJAHR) in billing document.. is it possible.
Thanks.
Regards,
MichaelHi Michel
If you feel that the fiscal year value should come in the billing document then you have to use a user exit USEREXIT_NUMBER_RANGE .
As this is a related to ABAP , you should give the inputs and ABAP'ers will give the number range as per our requirement .
Regards
Srinath -
Function not returning right value
I have this following function, based on the return value from this function, I am inserting a row into the GROUP_MAP table.
This function is returning a value greater than zero even though constrains would not let it select any rows.
It looks as if it is not applying the "AND STRING_CODE = String_Code" constraint to the result set. If there are two records matching the groupOID I passed it is returning two as the count(*). I checked by executing the query directly and I got 0 as the result. I sounds so strange. Is there any thing I am doing wrong ?
Thanks in Advance,
-Bhasker
FUNCTION FIND_CODE_GROUP (groupOID IN NUMBER, String_Code IN VARCHAR2)
RETURN NUMBER
AS
RETURN_VAL NUMBER(10);
BEGIN
RETURN_VAL := 0;
SELECT
COUNT(*)
INTO
RETURN_VAL
FROM
GROUP_MAP
WHERE
STRING_GROUP = groupOID AND STRING_CODE = String_Code;
DBMS_OUTPUT.PUT_LINE('RETURN_VAL:'|| RETURN_VAL || ' String code : ' || STRING_CODE);
RETURN(RETURN_VAL);
EXCEPTION
WHEN OTHERS THEN
RETURN(0);
END FIND_CODE_GROUP;FUNCTION FIND_CODE_GROUP (groupOID IN NUMBER, String_Code IN VARCHAR2)
RETURN NUMBER
AS
RETURN_VAL NUMBER(10);
BEGIN
RETURN_VAL := 0;
SELECT
COUNT(*)
INTO
RETURN_VAL
FROM
GROUP_MAP
WHERE
STRING_GROUP = groupOID AND STRING_CODE = String_Code;The second parameter to your function has the same name as the column name "STRING_CODE" in your table. You should change
the name of your second parameter to something other than "STRING_CODE". -
I deleted my uninstaller by accident how do get another one so that I can uninstall After Effects from my hard drive
Why did you do that? Certainly not standard procedure. Fortunately, there is a fix: Use the CC Cleaner Tool to solve installation problems | CC, CS3-CS6
-
How to get this function working
Dear buddies,
I created a function which should check if a certain code exists in a certain table.
Not sure how to get it done
create or replace function check_code(crs_code in varchar) return integer is
Result integer;
begin
if crs_code in (select cod_code from crs_code) then
result := 1;
else
result := 0;
end if;
return(Result);
end check_code;error: Compilation errors for FUNCTION CONV.CHECK_CODE
Error: PLS-00405: subquery not allowed in this context
Line: 6
Text: if crs_code in (select cod_code from crs_code) then
Error: PL/SQL: Statement ignored
Line: 6
Text: if crs_code in (select cod_code from crs_code) thenMy guess is that
create or replace function check_code(p_crs_code in varchar) return integer is
l_Result integer;
begin
SELECT COUNT(*)
INTO l_result
FROM dual
WHERE EXISTS (
SELECT 1
FROM crs_code
WHERE cod_code = p_crs_code
return( l_Result);
end check_code;should work but you've provided no table structure, no sample data, and no desired output so it's a bit unclear. Note that it is a really bad idea to have parameter names that match table names. My assumption is that CRS_CODE is both a table and a parameter name in your original function. Otherwise, I'm not sure what your SELECT statement is attempting to do.
Justin
Maybe you are looking for
-
Purchase order MENGE and NETWR comming blank 2LIS_02_ACC
Hi All, I am using the 2LIS_02_ACC Accounts assignments extractor(ECC6) for my BI7 lodas, and have used the MENGE(QTY) and NETWR(PO val) key figures. Aparantly when extracting Purchase Orders with Invoicing plans(Automated job to push invoices), it r
-
Trying to use two monitors with mac mini. One with HDMI and the with HDMI Mini port. Cannot get 2nd monitor to work Any suggestions
-
Is 720P 960 or 1280 pixels wide?
In FCP, the Preset choices for Sequence Settings include DVCPro 720P, which turns out to be 960 pixels wide. Panasonic brags that their D2 camera (which I just shot my first footage with) is 1280 pixels wide. Then there is the Canvas "view as square
-
How to assign drop down list in XML form to properties
Hi ALL, When i was creating <b>Edit form</b> in XML Forms Builder,for <b>Bulletin Board</b> Application, i had to assign two drop down list boxes to properties created under Content Management Configuration > Global Services > Property Metadata. Can
-
Javascript not working in any browser except Opera! Please help!
I don't know for what particular reason, javascript has stoppped working in all the browsers I have, (Safari, Firefox, Camino, OmniWeb,)except on Opera. Can anyone direct me in the right direction to solve this issue?