CASE instead of DECODE
With the help of the board I wrote a query using the DECODE function to display a full name correctly space with or without a middle name. Could I do that in a CASE statement? I am trying to come up with the best solution. I tried it but know I am missing something - here is what I have so far
Could I do it in a case statement? I am trying to think about the best way to approach this -
Here is what I wrote:
SELECT EMP_FNAME,
EMP_FNAME,
EMP_INITIAL,
CASE
WHEN EMP_INITIAL = null THEN
(EMP_FNAME||' '||EMP_LNAME)
ELSE CASE
WHEN EMP_INITIAL != null THEN
(EMP_FNAME||' '||EMP_INITIAL||' '||EMP_LNAME)
END
FROM EMPLOYEE
ORDER BY EMP_LNAME
I get this error - missing keyword
Any thoughts on how to correct this?
SELECT (CASE WHEN EMP_INITIAL IS NULL THEN (EMP_FNAME||' '||EMP_LNAME)
WHEN EMP_INITIAL IS NOT NULL THEN (EMP_FNAME||' '||EMP_INITIAL||' '||EMP_LNAME) END
END) longName
FROM EMPLOYEE
ORDER BY EMP_LNAME
Similar Messages
-
Case statement and Decode function both are not working in Select cursor.
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. On the other hand both the things work in just select statement.
See the first column in select (PAR_FLAG), I need to have this evaluated along with other fields. Can you please suggest some thing to make this work. And also I would like to
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Author : Amit Juneja
Date : 06/20/2011
Description:
Updates the Diamond MEMBER_MASTER table with the values from
INC.MEM_NJ_HN_MEMBER_XREF table.
declare
rec_cnt number(12) := 0;
commit_cnt number(4) := 0;
cursor select_cur is
Select DECODE(1,
(Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY'))),
'Y',
'N') PAR_FLAG,
H.SEQ_ELIG_HIST,
H.SEQ_MEMB_ID,
H.SEQ_SUBS_ID,
H.SUBSCRIBER_ID,
H.PERSON_NUMBER,
H.EFFECTIVE_DATE,
H.TERM_DATE,
H.TERM_REASON,
H.RELATIONSHIP_CODE,
H.SEQ_GROUP_ID,
H.PLAN_CODE,
H.LINE_OF_BUSINESS,
H.RIDER_CODE_1,
H.RIDER_CODE_2,
H.RIDER_CODE_3,
H.RIDER_CODE_4,
H.RIDER_CODE_5,
H.RIDER_CODE_6,
H.RIDER_CODE_7,
H.RIDER_CODE_8,
H.MEDICARE_STATUS_FLG,
H.OTHER_STATUS_FLAG,
H.HIRE_DATE,
H.ELIG_STATUS,
H.PREM_OVERRIDE_STEP,
H.PREM_OVERRIDE_AMT,
H.PREM_OVERRIDE_CODE,
H.SEQ_PROV_ID,
H.IPA_ID,
H.PANEL_ID,
H.SEQ_PROV_2_ID,
H.SECURITY_CODE,
H.INSERT_DATETIME,
H.INSERT_USER,
H.INSERT_PROCESS,
H.UPDATE_DATETIME,
H.UPDATE_USER,
H.UPDATE_PROCESS,
H.USER_DEFINED_1,
H.SALARY,
H.PEC_END_DATE,
H.REASON_CODE,
H.PEC_WAIVED,
H.BILL_EFFECTIVE_FROM_DATE,
H.BILLED_THRU_DATE,
H.PAID_THRU_DATE,
H.SUBSC_DEPT,
H.SUBSC_LOCATION,
H.USE_EFT_FLG,
H.BENEFIT_START_DATE,
H.SEQ_ENROLLMENT_RULE,
H.MCARE_RISK_ACCRETION_DATE,
H.MCARE_RISK_DELETION_DATE,
H.MCARE_RISK_REFUSED_DATE,
H.COMMENTS,
H.USER_DEFINED_2,
H.USER_DEFINED_3,
H.RATE_TYPE,
H.PCPAA_OCCURRED,
H.PRIVACY_ON,
H.PCP_CHANGE_REASON,
H.SITE_CODE,
H.SEQ_SITE_ADDRESS_ID,
PM.seq_prov_id rendered_prov
from hsd_member_elig_history H,
INC.PCP_REASSIGN_RPRT_DATA P,
hsd_prov_master PM
where P.subscriber_id = H.subscriber_id
and P.rendered_pcp = PM.provider_ID
and H.elig_status = 'Y'
and (H.term_date is NULL or H.term_date >= last_day(sysdate))
order by H.Seq_memb_id;
begin
for C in select_cur loop
rec_cnt := rec_cnt + 1;
update hsd_member_elig_history
set term_date = TRUNC(SYSDATE - 1),
term_reason = 'PCPTR',
update_datetime = SYSDATE,
update_user = USER,
update_process = 'TD33615'
where seq_elig_hist = C.seq_elig_hist
and seq_memb_id = C.seq_memb_id;
INSERT INTO HSD_MEMBER_ELIG_HISTORY
(SEQ_ELIG_HIST,
SEQ_MEMB_ID,
SEQ_SUBS_ID,
SUBSCRIBER_ID,
PERSON_NUMBER,
EFFECTIVE_DATE,
TERM_DATE,
TERM_REASON,
RELATIONSHIP_CODE,
SEQ_GROUP_ID,
PLAN_CODE,
LINE_OF_BUSINESS,
RIDER_CODE_1,
RIDER_CODE_2,
RIDER_CODE_3,
RIDER_CODE_4,
RIDER_CODE_5,
RIDER_CODE_6,
RIDER_CODE_7,
RIDER_CODE_8,
MEDICARE_STATUS_FLG,
OTHER_STATUS_FLAG,
HIRE_DATE,
ELIG_STATUS,
PREM_OVERRIDE_STEP,
PREM_OVERRIDE_AMT,
PREM_OVERRIDE_CODE,
SEQ_PROV_ID,
IPA_ID,
PANEL_ID,
SEQ_PROV_2_ID,
SECURITY_CODE,
INSERT_DATETIME,
INSERT_USER,
INSERT_PROCESS,
UPDATE_DATETIME,
UPDATE_USER,
UPDATE_PROCESS,
USER_DEFINED_1,
SALARY,
PEC_END_DATE,
REASON_CODE,
PEC_WAIVED,
BILL_EFFECTIVE_FROM_DATE,
BILLED_THRU_DATE,
PAID_THRU_DATE,
SUBSC_DEPT,
SUBSC_LOCATION,
USE_EFT_FLG,
BENEFIT_START_DATE,
SEQ_ENROLLMENT_RULE,
MCARE_RISK_ACCRETION_DATE,
MCARE_RISK_DELETION_DATE,
MCARE_RISK_REFUSED_DATE,
COMMENTS,
USER_DEFINED_2,
USER_DEFINED_3,
RATE_TYPE,
PCPAA_OCCURRED,
PRIVACY_ON,
PCP_CHANGE_REASON,
SITE_CODE,
SEQ_SITE_ADDRESS_ID)
values
(hsd_seq_elig_hist.nextval,
C.SEQ_MEMB_ID,
C.SEQ_SUBS_ID,
C.SUBSCRIBER_ID,
C.PERSON_NUMBER,
trunc(SYSDATE),
C.TERM_DATE,
C.TERM_REASON,
C.RELATIONSHIP_CODE,
C.SEQ_GROUP_ID,
C.PLAN_CODE,
C.LINE_OF_BUSINESS,
C.RIDER_CODE_1,
C.RIDER_CODE_2,
C.RIDER_CODE_3,
C.RIDER_CODE_4,
C.RIDER_CODE_5,
C.RIDER_CODE_6,
C.RIDER_CODE_7,
C.RIDER_CODE_8,
C.MEDICARE_STATUS_FLG,
C.OTHER_STATUS_FLAG,
C.HIRE_DATE,
C.ELIG_STATUS,
C.PREM_OVERRIDE_STEP,
C.PREM_OVERRIDE_AMT,
C.PREM_OVERRIDE_CODE,
C.SEQ_PROV_ID,
C.IPA_ID,
C.PANEL_ID,
C.SEQ_PROV_2_ID,
C.SECURITY_CODE,
SYSDATE,
USER,
'TD33615',
SYSDATE,
USER,
'TD33615',
C.USER_DEFINED_1,
C.SALARY,
C.PEC_END_DATE,
C.REASON_CODE,
C.PEC_WAIVED,
C.BILL_EFFECTIVE_FROM_DATE,
C.BILLED_THRU_DATE,
C.PAID_THRU_DATE,
C.SUBSC_DEPT,
C.SUBSC_LOCATION,
C.USE_EFT_FLG,
C.BENEFIT_START_DATE,
C.SEQ_ENROLLMENT_RULE,
C.MCARE_RISK_ACCRETION_DATE,
C.MCARE_RISK_DELETION_DATE,
C.MCARE_RISK_REFUSED_DATE,
C.COMMENTS,
C.USER_DEFINED_2,
C.USER_DEFINED_3,
C.RATE_TYPE,
C.PCPAA_OCCURRED,
C.PRIVACY_ON,
C.PCP_CHANGE_REASON,
C.SITE_CODE,
C.SEQ_SITE_ADDRESS_ID);
commit_cnt := commit_cnt + 1;
if (commit_cnt = 1000) then
dbms_output.put_line('Committed updates for 1000 records.');
commit;
commit_cnt := 0;
end if;
end loop;
commit;
dbms_output.put_line('Total number of MEMBER_ELIG_HISTROY records inserted : ' ||
rec_cnt);
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
end;user10305724 wrote:
I have tried both the Case statement and Decode function in Select cursor, but both the things are not working. Please define what you mean by not working even if your computer screen is near the internet we can't see it.
You should also look at the FAQ about how to ask a question
SQL and PL/SQL FAQ
Particularly *9) Formatting with {noformat}{noformat} Tags* and posting your version.
know the reason why decode is not working, I heard some where Case statement do not work with 8i.
Does this mean you are using 8i? Then scalar sub queries - selects within the select list, are not supported, along with CASE in PL/SQL.
Select DECODE(1,
* (Select 1
from hsd_prov_contract R
where R.seq_prov_id = PM.seq_prov_id
and R.line_of_business = H.line_of_business
and R.PCP_FLAG = 'Y'
and R.participation_flag = 'P'
and SYSDATE between R.EFFECTIVE_DATE AND
NVL(R.TERM_DATE,
TO_DATE('31-DEC-9999', 'DD-MON-YYYY')))*,
'Y',
'N') PAR_FLAG,
>
exception
when others then
raise_application_error(-20001,
'An error was encountered - ' || sqlcode ||
' -error- ' || sqlerrm);
http://tkyte.blogspot.com/2008/01/why-do-people-do-this.html -
Regarding case statement and decode function
Hi Experts,
I have question.....regarding case statement and decode statement....
Can you please explain me that which one will be efficient,to place in insert statement...
insert statement(
(case when ........then
case when ....then
else
end)
else
end)
or
insert statement(
case when.....then
decode(....)
else
end)
Can you people explain me which one is more efficient method?
Thanks in advance.......The are major differences to talk about in case of CASE vs DECODE, but performance wise both are pretty much the same.
Have a look at Tom's thread
Ask Tom "better performance - case or decode"
I would suggest to use CASE whenever possible. Don't worry about the performance part. -
ADF faces frames problem tree menu encode called instead of decode
Sorry , till now i have no investigated the problem completely
Message was edited by:
K.PenevSorry , but it's difficult to be achieved - i must send you the whole system.
I have noticed some of the circumstances when the problem appears;
I dont know , how it is connected but it is a fact:
When execute rollback on opening dialog window (in prepareModel);
If i set some view object attributes and close dialog
If i make a new record and commit
I forget to mention also that the View Objects used in the dialog window which i open is located in different application module then the main page content.
The interesting here is that i have another similar situation (opening dialog with own app module)which in the most of the time work (but also sometimes appear the same problem)?!?
Sorry that i cannot be more precise , but It 's very difficult to catch from where exactly the problem comes.
Is it possible on return from dialog and release some appl module into the pool , the menu page somehow to be destroyed and as result encode to be called instead of decode?
Message was edited by:
K.Penev -
Hi,
How to convert this decode statements into CASE WHEN syntax
phone_v := DECODE (c_rec.phone, NULL, NULL, DECODE ( is_it_number (c_rec.phone), 'F', DBMS_RANDOM.STRING ('a', LENGTH (c_rec.phone)), SUBSTR (DBMS_RANDOM.VALUE, 2, LENGTH (c_rec.phone))));
thanks, BcjThat should be easiest written like the following :
phone_v := DECODE (c_rec.phone
, NULL, NULL
, DECODE ( is_it_number (c_rec.phone)
, 'F', DBMS_RANDOM.STRING ('a', LENGTH (c_rec.phone))
, SUBSTR (DBMS_RANDOM.VALUE, 2, LENGTH (c_rec.phone))
);Is it ?
Nicolas. -
Help in changing plsql case when to decode
Hi,
Can anyone help me to change this sql for it to use decode function instead of case when? Below is the sql code
Thanks in advance.
SELECT
parts,
weeks,
SUM(qty) qty
FROM (
SELECT
CASE
WHEN ((is_tbd = 'yes' AND is_tbd_order = 'no') OR ex_fac_date > v_asofdate + 131) THEN 'tbd'
WHEN ex_fac_date BETWEEN v_asofdate - 1 AND v_asofdate + 5 AND is_tbd = 'no' THEN 'wk1'
WHEN ex_fac_date BETWEEN v_asofdate + 6 AND v_asofdate + 12 AND is_tbd = 'no' THEN 'wk2'
WHEN ex_fac_date BETWEEN v_asofdate + 13 AND v_asofdate + 19 AND is_tbd = 'no' THEN 'wk3'
WHEN ex_fac_date BETWEEN v_asofdate + 20 AND v_asofdate + 26 AND is_tbd = 'no' THEN 'wk4'
WHEN (ex_fac_date < v_asofdate - 1) AND is_tbd = 'no' THEN 'past_due'
END weeks,
ffdate,
parts,
SUM(qty) qty
FROM
delivery)I can't use case because my oracle is 8i,Please join 21st Century
Can you help me checnge this to if then elsif insteadSQL does does contain IF statement! -
Hello,
I am used to using case within my select statements, I need to use decode if possible for this one. Its for a record group within in forms, and the case statement is not working. Could someone please help me write this with decode instead of case?
SELECT CASE
WHEN months_between(sysdate , c.birth_dt) BETWEEN 168 AND 216 THEN 1 --14 to 18
WHEN months_between(sysdate , c.birth_dt) BETWEEN 217 AND 252 THEN 2 --18 to 21
WHEN months_between(sysdate , c.birth_dt) BETWEEN 253 AND 780 THEN 3 --21 to 65
WHEN months_between(sysdate , c.birth_dt) > 780 then 4 --older than 65
ELSE NULL END
FROM mw_clients cThanks in advance!
Jim P.Below solution is very easy to write.
But I think that performance will be bad.Not really as dual is internal to the SQL engine so there's little difference between the two...
SQL> ed
Wrote file afiedt.buf
1 with mw_clients as (select add_months(sysdate,-200) as birth_dt from dual union
2 select add_months(sysdate,-220) from dual union
3 select add_months(sysdate,-400) from dual)
4 -- END OF TEST DATA
5 select 4+
6 decode(Least(months_between(sysdate , c.birth_dt),168),
7 168,decode(greatest(months_between(sysdate , c.birth_dt),216),
8 216,-3,0)
9 ,0)+
10 decode(Least(months_between(sysdate , c.birth_dt),217),
11 217,decode(greatest(months_between(sysdate , c.birth_dt),252),
12 252,-2,0)
13 ,0)+
14 decode(Least(months_between(sysdate , c.birth_dt),253),
15 253,decode(greatest(months_between(sysdate , c.birth_dt),780),
16 780,-1,0)
17 ,0) as result
18* from mw_clients c
SQL> /
RESULT
3
2
1
Elapsed: 00:00:00.01
Execution Plan
Plan hash value: 1277652150
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 3 | 27 | 9 (34)| 00:00:01 |
| 1 | VIEW | | 3 | 27 | 9 (34)| 00:00:01 |
| 2 | SORT UNIQUE | | 3 | | 9 (78)| 00:00:01 |
| 3 | UNION-ALL | | | | | |
| 4 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 5 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 6 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
Statistics
1 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
456 bytes sent via SQL*Net to client
396 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
3 rows processed
SQL>
SQL> ed
Wrote file afiedt.buf
1 with mw_clients as (select add_months(sysdate,-200) as birth_dt from dual union
2 select add_months(sysdate,-220) from dual union
3 select add_months(sysdate,-400) from dual)
4 -- END OF TEST DATA
5 select c.birth_dt,
6 (select 1 from dual where months_between(sysdate , c.birth_dt) BETWEEN 168 AND 216 union
7 select 2 from dual where months_between(sysdate , c.birth_dt) BETWEEN 217 AND 252 union
8 select 3 from dual where months_between(sysdate , c.birth_dt) BETWEEN 253 AND 780 union
9 select 4 from dual where months_between(sysdate , c.birth_dt) > 780
10 ) as result
11* from mw_clients c
SQL> /
BIRTH_DT RESULT
06/08/1974 10:15:39 3
06/08/1989 10:15:39 2
06/04/1991 10:15:39 1
Elapsed: 00:00:00.01
Execution Plan
Plan hash value: 706016619
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 3 | 27 | 9 (34)| 00:00:01 |
| 1 | SORT UNIQUE | | 4 | | 12 (84)| 00:00:01 |
| 2 | UNION-ALL | | | | | |
|* 3 | FILTER | | | | | |
| 4 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
|* 5 | FILTER | | | | | |
| 6 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
|* 7 | FILTER | | | | | |
| 8 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
|* 9 | FILTER | | | | | |
| 10 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 11 | VIEW | | 3 | 27 | 9 (34)| 00:00:01 |
| 12 | SORT UNIQUE | | 3 | | 9 (78)| 00:00:01 |
| 13 | UNION-ALL | | | | | |
| 14 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 15 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 16 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
3 - filter(MONTHS_BETWEEN(SYSDATE@!,:B1)<=216 AND
MONTHS_BETWEEN(SYSDATE@!,:B2)>=168)
5 - filter(MONTHS_BETWEEN(SYSDATE@!,:B1)<=252 AND
MONTHS_BETWEEN(SYSDATE@!,:B2)>=217)
7 - filter(MONTHS_BETWEEN(SYSDATE@!,:B1)<=780 AND
MONTHS_BETWEEN(SYSDATE@!,:B2)>=253)
9 - filter(MONTHS_BETWEEN(SYSDATE@!,:B1)>780)
Statistics
1 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
538 bytes sent via SQL*Net to client
396 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
3 rows processed
SQL> ed -
Lookup Code value switch automatically in case of same decode value
Hi,
We have create a simple Lookup type UDF on OIM user profile. The lookup value contains the different code value for same decode value like 1-->Project Manager, 2-->Project Manager, 3-->Project Manager. Now the issue is that if someone goes form the web console and edit the user (just click on edit and then save it back without making any change), this lookup field value switch to default code value for 'Project Manager'. Let's say if OIM internally assume 2 as default code value for 'Project Manager' (not sure how OIM maintains it internally), If we follow the edit process for a user who has '3' as code value then after save the value in the database change to '2'. The same happens for user with '1' as code value as well. Now mapping multiple code values to same decode is very common where you need to integrate OIM with legacy systems where multiple key values assigned to the Job title/positions with same description. Has anyone came across this scenerio in past and found any solution. Please help. We tried by using lookup querry as well but no luck. We are using 9102 BP02 OIM. ThanksFirst thing first
Why do you need 3 entries in the lookup for the same vaule? It's only a confusion to the end-user to select which one.
Now....
In case you have some compelling situation, just try to have some differentiation for the decode value; like 1~Project Manager, 2~Project Manager, 3~Project Manager. This way confusion may be obviated and so as the issue. -
Using CASE insted of DECODE in Control File
Hi,
I have seen a lot of examples of DECODE in a Control File. I know how to use DECODE, but I prefer CASE. Does anyone know, if it is allowed, and is the synatx the same as standard CASE statements. I am going to try it, but this is just so that I don't try all different variations and then find that it is not available.
Another thought is could you use an IN List Statement.
Something like IF :col1 IN('A', 'AB') THEN "wow".
I know the IN is going to be jsut a WISH...
--Thanks
Message was edited by:
pshah2kYep, here's an example:
LOAD DATA
INFILE '/home/ennisbi/DBLOAD/data/xxx_2000.csv'
APPEND INTO TABLE STG_VOLUME
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
EFFECTIVE_DATE DATE "YYYYMM",
SSB_ID,
SEDOL_OR_CUSIP,
AVG_BID_ASK_SPREAD_DIV2,
AVG_SHARE_VOLUME,
ANNUALIZED_VOLATILITY "CASE WHEN :ANNUALIZED_VOLATILITY='NC' then 0 else TO_NUMBER(:ANNUALIZED_VOLATILITY) end" -
Converting CASE st. to decode
Hi, I'm using Oracle 8.0.5, apparently it does not support the CASE function. SOmeone let me know if this is incorrect, but I can't get it to work.
My question is I'm trying to write a fairly simple decode or CASE statement that says,
SELECT e.poid_type,
et.*,
CASE WHEN et.rec_id = 840 AND e.poid_type = '/event/billing/cycle/forward/' THEN et.amount
ELSE 0
end cforward,
a.account_no
FROM ACCOUNT_T a, EVENT_T e, EVENT_TOTAL_T et
WHERE a.account_no = '0040' AND a.poid_id0=e.account_obj_id0
AND et.obj_id0(+)=e.poid_id0
AND et.rec_id = 840;
Here is my attempt at a decode statement, but it does not work.
DECODE(et.rec_id, 840,
DECODE(e.poid_type, '/event/billing/cycle/forward', et.amount, 0), 0) cycle_forward,
When I use this it prints all zero's.
Basically, what I am trying to do is say when event_total_t rec_id = 840, and event_t poid_type = '/event/billing/cycle/forward' then display the event_total_t amount. I want to do this in the select statement, without using a where clause.
Thanks for any help on this.Your DECODE translation is fine, but you do want one more outer join indicator on your query:
AND et.rec_id (+)= 840 -
Hi everybody,
I have column in Discoverer like this one shown bellow:
DECODE(Vrstapolise,1,'Aktivno',4,'Kapitalisano',3,DECODE(Stanjepreisteka,'Aktivno','Aktivno','Kapitalisano','Kapitalisano',NULL),NULL)
I want to make the same column with same functionality in OBI BMM. Is it possible? If it is possible, how to do that?
Thanks in advance.As Sai said use Case stmt, your code will something like this
Case Vrstapolise
When 1 then 'Aktivno'
When 4 then 'Kapitalisano'
When 3 then
Case Stanjepreisteka
when 'Aktivno' then 'Aktivno'
when 'Kapitalisano' then 'Kapitalisano'
else Null end
else Null End
Thanks,
Vino -
Hi All,
I am trying to use a CASE logic inside a DECODE function while selecting data from a table. But unable to get the correct syntax. Is there something that I might be missing?
My snippet looks like -
SELECT DECODE(CASE WHEN COL2 > 10 THEN 'HIGH', WHEN COL2 < 10 THEN 'LOW' END RANGEVALUE, 'HIGH', 'YES','LOW','NO','N/A')
FROM TEST_TABLE;My table looks like -
TEST_TABLE
COL1 VARCHAR2(10),
COL2 NUMBER(8,0)Chaitanya wrote:
My snippet looks like -
SELECT DECODE(
CASE
WHEN COL2 > 10 THEN 'HIGH'
WHEN COL2 < 10 THEN 'LOW'
END,
'HIGH','YES',
'LOW','NO',
'N/A'
FROM TEST_TABLE
/But it is an overkill:
SELECT CASE
WHEN COL2 > 10 THEN 'YES'
WHEN COL2 < 10 THEN 'NO'
ELSE 'N/A'
END
FROM TEST_TABLE
/Or, if you wnat to use DECODE:
SELECT DECODE(
SIGN(COL2 - 10)
1,'YES'
-1,'NO',
'N/A'
FROM TEST_TABLE
/SY. -
How to convert the compund case statement into decode statement
(CASE
WHEN FRCST = 0 AND SALE = 0 THEN 'No transaction '
WHEN FRCST = 0 AND SALE <>0 THEN 'Sale ag. Nil Forecast : '||SALE||' Kgs'
WHEN FRCST<> 0 AND SALE = 0 THEN 'No Sale ag. Forecast : '||FRCST||' Kgs'
WHEN FRCST<>0 AND SALE<>0 AND DIFF=0 THEN 'No Variance'
ELSE TO_CHAR(ROUND((DIFF/FRCST),2))||'%'
END)VARIANCE
How to convert this tatement to decode statement ?
YogeshDecode(FRCST,0,DECODE(SALE,0,'nO TRANSACTION','SALE AGAINST NIL FORECAST'),DECODE(SALE,0,'NO SALE AGAINST FORECAST',
DECODE(|SALE-FORECAST|,0,'NO VARIANCE',TO_CHAR(ROUND((DIFF/FRCST),2))||'%')))As per me whole case can be replaced by above decode -
Hi,
In this query i have been using CASE for some conditions. Can i use DECODE in place of CASE? The case statement is in bold.
SELECT b.cmp_snam_e Buyer_Name,
a.code Buyer_Ref,
b.enter_dt Open_Date,
NVL(a.new_ocl,0) OCL_US$,
a.dec_date_g Last_Revision,
a.new_exp Next_Update,
b.st1_ref Stat_Ref,
initcap(c.mtb_desc) Status,
d.source_code Agency_Code,
d.inf_surs Info_Source_Code,
d.cbc_req_dt Date_Requested,
d.crp_date Date_Received,
CASE
WHEN ( a.os_rec_status IN ('PC') )
THEN a.os_rec_ref
WHEN (a.os_rec_status) IN ('IT','GA','CL','AR')
THEN
*(SELECT pcy_ref*
FROM con_cla_sub_apr_view
WHERE CLA_REF = A.OS_REC_REF
AND byr_ref = a.code
AND cla_sub =
*(SELECT MAX(CLA_SUB) FROM con_cla_sub_apr_view WHERE CLA_REF = A.OS_REC_REF*
END AS Policy_Ref, e.hst_remark
FROM ocl_submission a,
companies b,
master_tbl c,
cmp_by_crp d,
buyer_remarks e
WHERE A.code = B.cmp_ref
AND b.st1_ref = c.mtb_ref
AND a.code = d.cmp_ref
AND a.code = e.hst_byr_ref(+)
AND B.cmp_type = 'BYR'
AND B.st1_ref = 'ST1-00054'
AND (b.enter_dt >= :FROM_DATE
AND b.enter_dt <= :TO_DATE)
AND A.SEQ =
(SELECT MAX(SEQ) - 1 FROM ocl_submission WHERE CODE = B.cmp_ref AND SEQ != 1
AND d.srno =
(SELECT MAX(TO_NUMBER(srno))
FROM cmp_by_crp
WHERE cmp_ref = a.code
AND srno != 1
AND (e.hst_byr_ref, e.hst_byr_sub, e.SEQ) IN
SELECT hst_byr_ref, hst_byr_sub, MAX(SEQ) FROM buyer_remarks WHERE
(hst_byr_ref, hst_byr_sub) IN
(SELECT hst_byr_ref, MAX(hst_byr_sub)
FROM buyer_remarks WHERE hst_byr_ref= a.code
GROUP BY hst_byr_ref)
GROUP BY hst_byr_ref, hst_byr_sub
);Hi,
You can always use DECODE instead of CASE (and you can always use CASE instead of DECODE, and almost always should).
I agree with Someoneelse and Alex: this is a textbook example of a situation in which CASE is better than DECODE.
DECODE ( a.os_rec_status
, 'PC' , a.os_rec_ref
, DECODE ( INSTR ( ',IT,GA,CL,AR,'
, ',' || a.os_rec_status || ','
, 0 , NULL
SELECT pcy_ref
FROM con_cla_sub_apr_view
WHERE CLA_REF = A.OS_REC_REF
AND byr_ref = a.code
AND cla_sub = (
SELECT MAX (CLA_SUB)
FROM con_cla_sub_apr_view
WHERE CLA_REF = A.OS_REC_REF
)This assumes that os_rec_status never contains commas.
I can't imagine any situation in which DECODE would be significantly faster that CASE.
I can imagine lots of situations in which CASE is significantly faster than DECODE.
I expect, in this situation, CASE is only slightly faster than DECODE. -
Function or pseudo-column 'DECODE' may be used inside a SQL statement only
Hi everyone!
I got the error in the subject concernig the follow piece of sql code
+ v_str_sql_body :=
v_str_sql_body || ' and cod_entita ='
|| decode(cur.cod_entita_a,str_all,cur_ent.cod_entita,cur.cod_entita_a);
execute immediate v_str_sql_body; +
I can't understand what's the problem with it.
Can anyone help me to find the error?
Thank you in advance!
Samuel
Edited by: 996508 on 27-mar-2013 5.29Hi, Samuel,
Welcome to the forum!
996508 wrote:
Hi everyone!
I got the error in the subject concernig the follow piece of sql code
+ v_str_sql_body :=
v_str_sql_body || ' and cod_entita ='
|| decode(cur.cod_entita_a,str_all,cur_ent.cod_entita,cur.cod_entita_a);
execute immediate v_str_sql_body; +
I can't understand what's the problem with it.Sorry, I can't understand what you're trying to do.
Whenever you have a problem, post a complete script that people can run to re-create the problem and test their ideas. Include your complete PL/SQL code (if the problem involves PL/SQL) including code to call your procedure, a little sample data (CREATE TABLE and INSERT statements) for any tables involved, and the results you want the given sample data.
Always say which version of Oracle you have (e.g., 11.2.0.3.0).
See the forum FAQ {message:id=9360003}
Can anyone help me to find the error?As the error message says, you can only use DECODE in a SQL statement. That is:
v_str_body := v_str_body
|| ' and cod_entita = '
|| DECODE (...);is always an error, no matter what ... stands for.
Perhaps you meant to have the DECODE inside the string literal.
If not, then (depending on your version) you can use CASE instead of DECODE:
v_str_body := v_str_body
|| ' and cod_entita = '
|| CASE cur.cod_entita_a
WHEN str_all THEN cur_ent.cod_entita
ELSE cur.cod_entita_a
END;Anything that DECODE can do, CASE can do, too.
DECODE is sometimes a little simpler than CASE to use, but only a little. It is never faster than CASE.
CASE is sometimes much, much simpler to use than DECODE, and sometimes much faster, too.
I won't say never use DECODE; but I will say rarely use DECODE. Use CASE instead, unless you have a specific reason why DECODE is better in that particular situation.
In PL/SQL, you can also use IF ... THEN ... ELSE in places where you might want to use DECODE or CASE.
For example:
v_str_body := v_str_body
|| ' and cod_entita = ';
IF cur.cod_entita_a = str_all
THEN
v_str_body := v_str_body
|| cur_ent.cod_entita;
ELSE
v_str_body := v_str_body
|| cur.cod_entita_a;
END IF;Edited by: Frank Kulash on Mar 27, 2013 5:59 PM
Added IF ... THEN ... ELSE example.
Maybe you are looking for
-
How to restore the default web configuration in Lion
Hi guys so I was messing around with apache/php/mysql, and after trying to enable openssl I think i made a mistake because when I try to enable web sharing by checking off the checkbox under system preferences it just turns back off right away. If i
-
Transparent Buttons in Java Swing?
Hi! I want to create transparent Buttons using java swing for my desktop application. Can any one tell me how to do it?
-
GET element(s) by an attributes value
Hi, Is it possible to get an element of a node by the value of 1 attribute? I know there is a method getelement() where you can specify an index,but I don't find a method where you can specify the attribute's name and value. Now I have to loop trough
-
Database won't automatically open in win2000
When we boot up numerous windows 2000 machines the database starts fine through the services menu BUT it doesn't automatically open as it did with windowsNT. This is for 8.1.7 and 8.1.6. Anyone got any ideas? Thanks
-
Have Wi-Fi connection but no IP address (Status: IP address no longer valid)
Dear experts, I can connect my Pearl 9100 to my home Wi-Fi network but I cannot connect to the internet. For example, when I run the the Browser I get the message "Unable to connet to the Internet..." . I can see on my router that Pearl has been as