| CREATING FUNCTION FOR "CASE STATEMENT" [message #574199] |
Mon, 07 January 2013 20:27  |
 |
nischalinn
Messages: 84 Registered: May 2012 Location: nepal
|
Member |
|
|
CREATE TABLE JAN07
(
COL1 VARCHAR2(50),
COL2 VARCHAR2(50),
COL3 VARCHAR2(50)
)
INSERT ALL
INTO JAN07 VALUES ('100A','{XXX}','888')
INTO JAN07 VALUES ('ZZZ','ZZC}','00C')
INTO JAN07 VALUES ('TRUE','ABJ','TRUE')
SELECT * FROM DUAL;
I have to create a function for the below case. The "PARAM1" is the columns i.e. COL1,COL2,COL3.
CASE SUBSTR(PARAM1, -1)
WHEN '{' THEN REPLACE(PARAM1, '{', '0')
WHEN 'A' THEN REPLACE(PARAM1, 'A', '1')
WHEN 'B' THEN REPLACE(PARAM1, 'B', '2')
WHEN 'C' THEN REPLACE(PARAM1, 'C', '3')
WHEN '}' THEN '-' || REPLACE(PARAM1, '}', '0')
WHEN 'J' THEN '-' || REPLACE(PARAM1, 'J', '1')
WHEN 'K' THEN '-' || REPLACE(PARAM1, 'K', '2')
ELSE
PARAM1
When I pass the column name it should provide me the o/p as:
Quote:
1,-0,8
Z,-0,3
E,-1,E
How can I do it?
|
|
|
|
|
|
|
|
|
|
| Re: CREATING FUNCTION FOR "CASE STATEMENT" [message #574203 is a reply to message #574202] |
Mon, 07 January 2013 21:21   |
 |
nischalinn
Messages: 84 Registered: May 2012 Location: nepal
|
Member |
|
|
I've created the function but I think the "SUBSTR" is not working in my function. I can not figure out the error.
CREATE OR REPLACE FUNCTION rep_string (param1 IN varchar2)
RETURN VARCHAR2 IS
BEGIN
RETURN
CASE SUBSTR(PARAM1, -1)
WHEN '{' THEN REPLACE(PARAM1, '{', '0')
WHEN 'A' THEN REPLACE(PARAM1, 'A', '1')
WHEN 'B' THEN REPLACE(PARAM1, 'B', '2')
WHEN 'C' THEN REPLACE(PARAM1, 'C', '3')
WHEN '}' THEN '-' || REPLACE(PARAM1, '}', '0')
WHEN 'J' THEN '-' || REPLACE(PARAM1, 'J', '1')
WHEN 'K' THEN '-' || REPLACE(PARAM1, 'K', '2')
ELSE
PARAM1
END;
END;
/
|
|
|
|
| Re: CREATING FUNCTION FOR "CASE STATEMENT" [message #574204 is a reply to message #574203] |
Mon, 07 January 2013 22:05   |
 |
BlackSwan
Messages: 20245 Registered: January 2009
|
Senior Member |
|
|
>I've created the function but I think the "SUBSTR" is not working in my function. I can not figure out the error.
ERROR?
What Error?
I don't see any error.
SQL> select rep_string('COL1') from dual;
REP_STRING('COL1')
--------------------------------------------------------------------------------
COL1
Why is it that NOTHING in the function ever references table JAN07?
What is the relationship between the function REP_STRING & table JAN07?
[Updated on: Mon, 07 January 2013 22:22] Report message to a moderator
|
|
|
|
|
|
|
|