Include if else in sql statement [message #639929] |
Mon, 20 July 2015 08:15 |
|
hypesslearner
Messages: 15 Registered: July 2015
|
Junior Member |
|
|
Hi All
I have a column in a table called cost center .. I have a requirements as
If cost center is beginning with "S", "V" or "Y" then I need to map all the rows to PP_S_division for Portfolio
else I need map as "PH_A"& 4 last digits CostCenter &"_SIDIV""
I am able to do the second functionality as below
CAST('PH_A'||SUBSTR(COST_CTR,-4)||''_SIDIV'' AS VARCHAR2(80)) AS "Portfolio",
but how do I incorporate the first requirement in this . Please help.
Thanks in Advance,
OP
|
|
|
|
|
|
|
|
|
|
Re: Include if else in sql statement [message #639999 is a reply to message #639946] |
Tue, 21 July 2015 04:21 |
|
hypesslearner
Messages: 15 Registered: July 2015
|
Junior Member |
|
|
Thanks all . I tried to do this with function. However I had a tough time to compile this .
Please help
CREATE OR REPLACE FUNCTION FUN_G_P_CTR
(
COST_CTR in varchar(200)
TEMP in varchar(200)
)
RETURN VARCHAR2 is MAP_PROD varchar(200);
BEGIN
IF (SUBSTR(COST_CTR,1) = 'S' OR SUBSTR(COST_CTR,1) = 'V' OR SUBSTR(COST_CTR,1) = 'Y') THEN
-- IF TEMP = 'S' OR TEMP = 'V' OR TEMP = 'Y') THEN
MAP_PROD := 'PH_BB-RM_SIV';
ELSE
MAP_PROD := 'PH_A'||SUBSTR(COST_CTR,-4)||'_SIDIV'
END IF;
RETURN MAP_PROD;
END FUN_G_P_CTR;
CM: end code tags must start with a /
I've fixed it this time
[Updated on: Tue, 21 July 2015 04:25] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
|
Re: Include if else in sql statement [message #640022 is a reply to message #640021] |
Tue, 21 July 2015 07:46 |
mnitu
Messages: 159 Registered: February 2008 Location: Reims
|
Senior Member |
|
|
There is no need for a PL/SQL function here
Select COST_CTR,
Case
When SubStr(COST_CTR,1) In ('S', 'V', 'Y') Then 'PH_BB-RM_SIV'
Else 'PH_A'||SUBSTR(COST_CTR,-4)||'_SIDIV'
End As "PROD"
From STAGING_TABLE1
|
|
|
|
|
|
|
|
Re: Include if else in sql statement [message #640572 is a reply to message #640307] |
Thu, 30 July 2015 16:07 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
Why did you even use the function. It is a total waste of processor cycles. Use the query
Select COST_CTR,
Case
When SubStr(COST_CTR,1,1) In ('S', 'V', 'Y') Then 'PH_BB-RM_SIV'
Else 'PH_A'||SUBSTR(COST_CTR,-4)||'_SIDIV'
End As "PROD"
From STAGING_TABLE1
that mnitu provided. However if you are uncomfortable with case statements then you can do
select cost_ctr,decode(substr(cost_ctr,1,1),'S','PH_BB-RM_SIV',
'V','PH_BB-RM_SIV',
'Y','PH_BB-RM_SIV',
'PH_A'||SUBSTR(COST_CTR,-4)||'_SIDIV') PROD
FROM STAGING_TABLE1;
|
|
|