Home » SQL & PL/SQL » SQL & PL/SQL » Include if else in sql statement
Include if else in sql statement [message #639929] Mon, 20 July 2015 08:15 Go to next message
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 #639931 is a reply to message #639929] Mon, 20 July 2015 08:32 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/

BTW - the SQL language does NOT contain any IF statement
Re: Include if else in sql statement [message #639932 is a reply to message #639931] Mon, 20 July 2015 08:36 Go to previous messageGo to next message
hypesslearner
Messages: 15
Registered: July 2015
Junior Member
Ok thanks , Is there any other command that takes care of this kind of requirement ?

[Updated on: Mon, 20 July 2015 08:36]

Report message to a moderator

Re: Include if else in sql statement [message #639933 is a reply to message #639932] Mon, 20 July 2015 08:42 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
> Is there any other command that takes care of this kind of requirement ?
yes

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: Include if else in sql statement [message #639934 is a reply to message #639933] Mon, 20 July 2015 08:43 Go to previous messageGo to next message
hypesslearner
Messages: 15
Registered: July 2015
Junior Member
Ok, Can you please tell me what is that command .

Thanks
OP
Re: Include if else in sql statement [message #639938 is a reply to message #639934] Mon, 20 July 2015 08:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It is in the links you didn't read.

Re: Include if else in sql statement [message #639944 is a reply to message #639938] Mon, 20 July 2015 09:53 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@BS, the second link is broken.
Re: Include if else in sql statement [message #639946 is a reply to message #639944] Mon, 20 July 2015 10:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So here are the links:
OraFAQ Forum Guide
How to use [code] tags and make your code easier to read.
Re: Include if else in sql statement [message #639999 is a reply to message #639946] Tue, 21 July 2015 04:21 Go to previous messageGo to next message
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 #640001 is a reply to message #639999] Tue, 21 July 2015 04:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> CREATE OR REPLACE FUNCTION FUN_G_P_CTR
  2  (
  3  COST_CTR in varchar(200)
  4  TEMP in varchar(200)
  5  )
  6  RETURN VARCHAR2 is MAP_PROD varchar(200);
  7
  8  BEGIN
  9    IF (SUBSTR(COST_CTR,1) = 'S' OR SUBSTR(COST_CTR,1) = 'V' OR SUBSTR(COST_CTR,1) = 'Y') THEN
 10    -- IF TEMP = 'S' OR TEMP = 'V' OR TEMP = 'Y') THEN
 11      MAP_PROD := 'PH_BB-RM_SIV';
 12      ELSE
 13      MAP_PROD := 'PH_A'||SUBSTR(COST_CTR,-4)||'_SIDIV'
 14      END IF;
 15    RETURN MAP_PROD;
 16  END FUN_G_P_CTR;
 17  /

Warning: Function created with compilation errors.

SQL> sho err
Errors for FUNCTION FUN_G_P_CTR:
LINE/COL ERROR
-------- -------------------------------------------------------------------------------------
3/20     PLS-00103: Encountered the symbol "(" when expecting one of the following:
         := . ) , @ % default character


1/ Parameter type are not constrained (no length)
2/ ',' are missing between parameters

Fix that and then the possible other syntax error.

Refer to PL/SQL User's Guide and Reference for basic syntax.

Re: Include if else in sql statement [message #640003 is a reply to message #639999] Tue, 21 July 2015 04:36 Go to previous messageGo to next message
hypesslearner
Messages: 15
Registered: July 2015
Junior Member
Thanks

Here is the modified code .

CREATE OR REPLACE FUNCTION FUN_G_P_CTR
(
COST_CTR in varchar,
TEMP in varchar
) 
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;


Re: Include if else in sql statement [message #640004 is a reply to message #640003] Tue, 21 July 2015 04:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

VARCHAR is obsolete, use VARCHAR2 instead.

Re: Include if else in sql statement [message #640005 is a reply to message #640003] Tue, 21 July 2015 04:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Why are you using VARCHAR? You should use VARCHAR2 instead. Oracle strongly recommends that you should avoid using VARCHAR as it might change in future.
Re: Include if else in sql statement [message #640011 is a reply to message #640005] Tue, 21 July 2015 06:00 Go to previous messageGo to next message
hypesslearner
Messages: 15
Registered: July 2015
Junior Member
I am new to PLSQL . I have changed it now .

However I am trying to execute this function like this
select CAST(FUN_G_P_CTR(COST_CTR) AS VARCHAR2(80)) AS "PROD" from STAGING_TABLE1;


where COST_CTR is a column in the table STAGING_TABLE1. However this is failing saying

ORA-00904: "FUN_G_P_CTR": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 1 Column: 13
Re: Include if else in sql statement [message #640012 is a reply to message #640011] Tue, 21 July 2015 06:04 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So the function does not exist or is not accessible to the user that tries to execute it.

You MUST copy and paste your SQL*Plus session, the whole session, as I did above.

Re: Include if else in sql statement [message #640021 is a reply to message #640012] Tue, 21 July 2015 07:30 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Also your function has two parameters, you're only supplying one.
Re: Include if else in sql statement [message #640022 is a reply to message #640021] Tue, 21 July 2015 07:46 Go to previous messageGo to next message
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 #640023 is a reply to message #640022] Tue, 21 July 2015 07:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I bet this is an exercise not a real case.

Re: Include if else in sql statement [message #640100 is a reply to message #640023] Wed, 22 July 2015 05:36 Go to previous messageGo to next message
hypesslearner
Messages: 15
Registered: July 2015
Junior Member
Thanks all .. Its working now .. and this is not a exercise ..
Re: Include if else in sql statement [message #640105 is a reply to message #640100] Wed, 22 July 2015 07:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So did you apply the advice above: remove the function?

Re: Include if else in sql statement [message #640297 is a reply to message #640105] Mon, 27 July 2015 02:03 Go to previous messageGo to next message
hypesslearner
Messages: 15
Registered: July 2015
Junior Member
No, Included the function but used "case" and "when" instead of "if"
Re: Include if else in sql statement [message #640307 is a reply to message #640297] Mon, 27 July 2015 03:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You should, the performances should then be much better.

Re: Include if else in sql statement [message #640572 is a reply to message #640307] Thu, 30 July 2015 16:07 Go to previous message
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;
Previous Topic: NUMERIC DENOMINATION
Next Topic: Time period condition
Goto Forum:
  


Current Time: Fri Apr 26 09:08:20 CDT 2024