Case When instead of Decode
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, Bcj
That 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.
Similar Messages
-
SELECT v_startdate, v_enddate,
(CASE WHEN SYSDATE BETWEEN v_startdate AND v_enddate THEN
‘active’
ELSE
‘inactive’
END) status
FROM correction_tab;
Could you kindly guide us as to how can we use DECODE and get the desired output.
Thanks,
Rami Reddy.You can, like this.
However, the CASE seems a lot clearer to me so why bother with a DECODE?
sql> with correction_tab as
2 ( select trunc(sysdate)-1 as v_startdate, trunc(sysdate) as v_enddate from dual
3 union all
4 select trunc(sysdate) as v_startdate, trunc(sysdate)+1 as v_enddate from dual
5 union all
6 select trunc(sysdate)+1 as v_startdate, trunc(sysdate)+2 as v_enddate from dual
7 )
8 SELECT v_startdate
9 , v_enddate
10 , CASE
11 WHEN SYSDATE BETWEEN v_startdate AND v_enddate
12 THEN 'active'
13 ELSE 'inactive'
14 END status
15 , decode ( sign(sysdate-v_startdate), 1, decode(sign(v_enddate-sysdate), 1, 'active', 'inactive'), 'inactive')
as status2
16 FROM correction_tab
17 /
V_STARTDA V_ENDDATE STATUS STATUS2
12-DEC-12 13-DEC-12 inactive inactive
13-DEC-12 14-DEC-12 active active
14-DEC-12 15-DEC-12 inactive inactive -
Using case when statement or decode stament in where clause
hi gems..
i have a problem in the following query..
i am trying to use case when statement in the where clause of a select query.
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when '>' = '>' then 'cr.salary > 5000'
when '>' = '<' then 'cr.salary < 5000'
when '>' = '=' then 'cr.salary = 5000'
else null
end);
the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
thats why for running the query, i have put '>' in that place.
so the original query will look like this(for your reference):
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when variable = '>' then 'cr.salary > 5000'
when variable = '<' then 'cr.salary < 5000'
when variable = '=' then 'cr.salary = 5000'
else null
end);
so, in actual case,if the user selects '>' then the filter will be "where cr.salary > 5000"
if the user selects '<' then the filter will be "where cr.salary < 5000"
if the user selects '=' then the filter will be "where cr.salary = 5000"
but i am getting the error "ORA 00920:invalid relational operator"
please help..thanks in advance..Hi,
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where ( v_variable = 'bigger'
and cr.salary > 5000
or ( v_variable = 'less'
and cr.salary < 5000
or ( v_variable = 'eq'
and cr.salary = 5000
)Edited by: user6806750 on 22.12.2011 14:56
For some reason I can't write in sql '<', '>', '=' -
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! -
Using CASE within a function I wish to apply a second level of criteria within the CASE statement, something along the lines of the following:
CASE upper(option_in)
WHEN 'P' THEN decode(instr(value_in,'-')>1,value_in,plan_b)
WHEN ...
Looks nifty but doesn't work. Any suggestions would be appreciated.
Thanks,
DanWhy are you switching to DECODE? Just use CASE again:
case upper(option_in)
when 'P' then case
when instr(value_in, '-') > 1 then value_in
else plan_b
end
when ... -
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 -
working on SSRS report , pulling the data with Emp # I want to show with emp Name instead,
is there way to write case when syntax so it can show only emp name instead of emp #
for example
Emp # Emp Name
144 Kevin
want report to pick the name as Kevin instead of 144
any help will be great
thanskthanks for reply RSingh
I tried changing the query as you suggested
SELECT [EMP #], [EMP NAME], FIELD1, FIELD2 FROM TABLENAME
it did not work, getting error message
empLid table name JMGPAYEMPLOYEE and there is no emp name field
when I modify the query as per above getting error message
because I have 2 data set
1. Projid
2. Projgroupid
3. parameter I have date
below is my query
SELECT PROJTABLE.PROJID, PROJTABLE.PROJGROUPID, PROJTABLE.DATAAREAID, JMGPAYEMPLOYEE.EMPLID, PROJTRANSPOSTING.QTY, JMGPAYEMPLOYEE.PRICE,
PROJTABLE.DIMENSION2_, PROJTRANSPOSTING.POSTINGTYPE, PROJTRANSPOSTING.COSTSALES, PROJTRANSPOSTING.PROJTRANSDATE,
CATEGORYTABLE.CATEGORYNAME
FROM PROJTABLE INNER JOIN
PROJTRANSPOSTING ON PROJTABLE.PROJID = PROJTRANSPOSTING.PROJID AND PROJTABLE.DATAAREAID = PROJTRANSPOSTING.DATAAREAID
INNER JOIN
JMGPAYEMPLOYEE ON PROJTRANSPOSTING.DATAAREAID = JMGPAYEMPLOYEE.DATAAREAID AND
PROJTRANSPOSTING.EMPLITEMID = JMGPAYEMPLOYEE.EMPLID INNER JOIN
CATEGORYTABLE ON JMGPAYEMPLOYEE.DATAAREAID = CATEGORYTABLE.DATAAREAID AND
JMGPAYEMPLOYEE.EMPLID = CATEGORYTABLE.CATEGORYID
WHERE (PROJTRANSPOSTING.COSTSALES = 1) AND (PROJTRANSPOSTING.POSTINGTYPE = 121) AND (PROJTRANSPOSTING.PROJTRANSDATE >= @Paramfromdate) AND
(PROJTRANSPOSTING.PROJTRANSDATE <= @ParamTodate)
any advise will be helpful
thanks -
Using CASE WHEN in PL/SQL package
I am trying to convert the values in a selected column into 1 and 0 so that I can display all 1s in one column, all 0s in another. I am doing this in a PL/SQL package. However ORACLE compiler does not like the CASE construct.
Does anyone know how to group values in a column into several new columns. If CASE WHEN construct is not doable in PL/SQL, what alternatives are there? Thanks.
/******* My package starts here *******/
CREATE OR REPLACE PACKAGE TEST_NEED AS
PROCEDURE procTEST_NEED(STARTING_DATE IN VARCHAR2);
END CVRR_MON_NEED;
CREATE OR REPLACE PACKAGE BODY TEST_NEED
AS
PROCEDURE procTEST_NEED(STARTING_DATE IN VARCHAR2)
IS
TEST_START DATE := TO_DATE(STARTING_DATE,'MM/DD/YYYY');
CURSOR v_Cursor IS
SELECT A.D_CODE, A.M_CODE, TEST_START , C.C_NAME,C.P_ID,
SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 > 40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND B.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0 END) QUALIFIED
FROM A, B, C, D
WHERE A.ID = B.B_ID
AND RTRIM(A.P_CODE) = C.P_CODE
AND A.P_ID = D.P_ID
AND A.P_ID < 99999999999999999999
AND A.E_DATETIME < SYSDATE
GROUP BY A.D_CODE, A.M_CODE, TEST_START , C.C_NAME,C.P_ID;
v_RecordHolder v_Cursor%ROWTYPE;
BEGIN
OPEN v_Cursor;
FETCH v_CursorINTO v_RecordHolder ;
WHILE v_Cursor%FOUND LOOP
look for records in another table with matching keys of the cursor
if found then update by incrementing the existing values in the matching records with values of the current currsor row
else insert the current cursor row
FETCH v_Cursor INTO v_RecordHolder ;
END LOOP;
END procTEST_NEED;
END TEST_NEED;I am trying to convert the values in a selected
column into 1 and 0 so that I can display all 1s in
one column, all 0s in another. I am doing this in a
PL/SQL package. However ORACLE compiler does not
like the CASE construct.
Does anyone know how to group values in a column into
several new columns. If CASE WHEN construct is not
doable in PL/SQL, what alternatives are there?
Thanks.
CURSOR v_Cursor IS
SELECT A.D_CODE, A.M_CODE, TEST_START ,
, C.C_NAME,C.P_ID,
SUM(CASE WHEN MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 ANDB.B_CODE IN '11.1','222.2','272.4') THEN 1 ELSE 0
END) QUALIFIEDUse the Decode function. This has been around in oracle SQL for ages and works like a case construct.
You would do something like
select ...
sum( decode (MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 >
40 AND MONTHS_BETWEEN(SYSDATE, D.P_DOB)/12 <85 AND
B.B_CODE IN ('11.1','222.2','272.4') 1,0 ) -
Error creating view with CASE -- WHEN statement in SQL*Plus
I am using Oracle 8i 8.1.7
I have an Oracle view which uses CASE...WHEN statements.
The view compiles fine in DBA studio.
Using TOAD I saved the view as an *.sql file.
However, when I try to create the view in SQL*Plus I get the following error:
SP2-0734: unknown command beginning "CASE WHEN ..." - rest of line ignored.
According to the documentation CASE -- WHEN has been implemented since since Oracle 8i rel. 2 (8.1.6)Well I'm using 8.1.6.3 and CASE and DECODE both work for me:
SQL> create or replace view v_accs as select account_name, txn,
2 decode(credit, 0, 'DB', 'CR') t_type
3 from accs;
View created.
SQL> select * from v_accs;
ACCOUNT_NA TXN T_
APC 1 DB
ABC 2 DB
HJJ 3 DB
HJH 4 CR
HJK 5 CR
APC 6 DB
APC 7 DB
ABC 8 DB
ABC 9 DB
HJJ 10 DB
HJJ 11 DB
HJH 12 DB
HJH 13 DB
HJK 14 DB
HJK 15 CR
15 rows selected.
SQL> create or replace view v_accs as select account_name, txn,
2 case when credit = 0 then 'DB' else 'CR'end as t_type
3* from accs
View created.
SQL> select * from v_accs;
ACCOUNT_NA TXN T_
APC 1 DB
ABC 2 DB
HJJ 3 DB
HJH 4 CR
HJK 5 CR
APC 6 DB
APC 7 DB
ABC 8 DB
ABC 9 DB
HJJ 10 DB
HJJ 11 DB
HJH 12 DB
HJH 13 DB
HJK 14 DB
HJK 15 CR
15 rows selected.
SQL>
rgds, APC -
Using CASE WHEN THEN in dynamic Region Source area
Hi all,
I've been knocking my head around on this one for a while. Hope someone can show me some guidance. So far, the SQL Query in the Region Source works ok except after the THEN ''||'LINK'||'' . All text between the single quotes displays, and not just the hyperlink LINK. (My goal is to this simple test working, then go back an substitute in APP_ID, Page#, SESSION variables.) But I'm stuck with this problem. Searched the forum but this is as far as I got. Below is the code I'm using in the Region Source area. Perhaps a region attribute setting is needed but I'm unsure. Thanks for any assistance. -Mike
select DISTINCT
AWARD,
CASE
WHEN WINNER_NAME = 'India Retail Support Team'
THEN '<A HREF="http://www.hotmail.com">'||'LINK'||'</A>' *----------------HREF link (LINK) is working fine here but doesn't work ok in Region Source.
ELSE WINNER_NAME
END WN,
GBU_WINNER,
DORDER
from GBUSERVICEPACESETTERAWARDS
where FY = '2012'
and TYPE = 'Service Award'>
As previously requested, please update your forum profile with a real handle instead of "user10734329".
I've been knocking my head around on this one for a while. Hope someone can show me some guidance. So far, the SQL Query in the Region Source works ok except after the THEN ''||'LINK'||'' . All text between the single quotes displays, and not just the hyperlink LINK.Not clear from this what the problem is. It appears to me that LINK is all of the "text between the single quotes". What do you mean by "all text"? (An example on apex.oracle.com is a good way to resolve such ambiguities...)
The usual cause of unexpected results when the separation of concerns is breached and HTML is directly generated in report queries is having the Display Text As report column attribute wrongly set. Ensure it is Standard Report Column rather than the default Display as Text (escape special characters). -
Hi all!
I have a report in Discoverer Plus (Version 10.1.2.48.18) which contains 2 columns: One with actual spendings and one with budget figures. I want to make a third column which holds actual spendings in percentage of the budget. To do this, I need to make a calculation similar to:
CASE WHEN SUM(budget) <> 0 THEN SUM(spendings)/SUM(budget) ELSE NULL END
However, when I apply this calculation to the third column, my report don't return any numbers at all in any column.
What am I doing wrong? -- Is this a bug, and how should I solve my problem?
~MortenI can get something out if I do this (applying an aggregate function to it all):
SUM(CASE WHEN budget <> 0 THEN spendings/budget ELSE NULL END)
However, this is wrong (Summarizing these percentages doesn't give any meaning).
SQL lookes something like this:
SELECT /*+ NOREWRITE */ o100448.ACCOUNT as E100451,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) as E101004,MAX(o100448.ACCOUNTNUM) as as100473_100451_NEW,CASE WHEN ( SUM(o100862.BUDGET) ) <> 0 THEN ( SUM(o100862.AMOUNT) )/( SUM(o100862.BUDGET) ) ELSE NULL END as C_1,( SUM(o100862.BUDGET) )*o100448.SIGN as C_3,( SUM(o100862.AMOUNT) )*o100448.SIGN as C_2,GROUPING_ID(o100448.ACCOUNT,o100448.SIGN,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM')))) as GID
FROM TANGO.TANGO_ACCOUNTS o100448,
TANGO.TANGO_SUMS o100862
WHERE ( (o100862.ORG = o100448.ORG AND o100862.ACCOUNTNUM = o100448.SUBACCOUNTNUM))
AND (o100448.ACCOUNTNUM BETWEEN 30000 AND 79999)
AND (o100862.DIM = '50')
AND (o100448.ORG = 'bru')
GROUP BY GROUPING SETS(( o100448.ACCOUNT,o100448.SIGN,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) ),( o100448.ACCOUNT,(decode((ADD_MONTHS(o100862.DATE1,-4)),null,to_date(null, 'MMDDYYYY'),to_date(to_char(trunc((ADD_MONTHS(o100862.DATE1,-4)),'YYYY'),'YYYY') || '01','YYYYMM'))) ))
HAVING (GROUP_ID()=0)
ORDER BY GID DESC;
I tried to fire this SQL up in TOAD (or whatever SQL-tool you might have), and columns C_2 and C_3 are empty. Seems like I'm doing something awfully wrong here.....
Any ideas? (There must be some SQL sharks out there ;-p)
~Morten -
Returne between dates in case when
i want to return between dates values on case when clauses on where clauses
like
and
(case
when (cc.segment3 NOT like '4%' and cc.segment3 NOT like '5%')
then (between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr'))
when (cc.segment3 like '4%' or cc.segment3 like '5%')
then (between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr'))
end) h.default_effective_date
the problem is in the = operator but i don't know haw to use this
any help??ok here is the full coad
select
DECODE(SUBSTR (CC.segment1, 1, 1), 'J', 'OFFSHORE', 'I', 'OFFSHORE','IN COUNTRY')branch_nature,
decode(cc.segment1,'A6260','Head Office','Branches') branch_type,
cc.segment1,
decode (h.currency_code,'EGP','mahly','agnaby') currency_code ,
cc.segment3,
t.description,
CASE
when h.currency_code in('EGP') then sum(l.entered_cr)
else 0
end entered_cr,--sum(l.entered_cr) entered_cr,
CASE
when h.currency_code in('EGP') then sum(l.entered_dr)
else 0
end entered_dr, --sum(l.entered_dr) entered_dr,
CASE
when h.currency_code NOT in('EGP') then sum(l.accounted_cr)
else 0
end accounted_Dr,--0 accounted_Dr,
CASE
when h.currency_code NOT in('EGP') then sum(l.accounted_dr)
else 0
end accounted_cr --0 accounted_cr
from apps.gl_je_headers h,
apps.gl_je_lines L,
apps.gl_code_combinations cc,
apps.fnd_flex_values_tl t ,
applsys.fnd_user us,
apps.gl_je_batches b
where h.status = 'P'
and us.user_id = h.Created_by
--and h.currency_code in('EGP')
and l.je_header_id = h.je_header_id
and l.code_combination_id =cc.code_combination_id
and cc.segment3 = t.flex_value_meaning
and cc.segment3 in ('31000020','40505020')
and cc.segment1 in ('A5550','B0010')
-- and (DECODE(SUBSTR (CC.segment1, 1, 1), 'J', 'OFFSHORE', 'I', 'OFFSHORE','IN COUNTRY') =:P_branch_nature OR :P_branch_nature is NULL)
--and (decode(cc.segment1,'A6260','Head Office','Branches') = :P_BRANCH_TYPE OR :P_BRANCH_TYPE is NULL)
and t.description is not null
and t.language ='AR'
and h.je_batch_id = b.je_batch_id
and
(case
when (cc.segment3 NOT like '4%' and cc.segment3 NOT like '5%')
then (between to_date(:P_Start_date,'dd/mm/rrrr') and to_date(:P_END_date,'dd/mm/rrrr'))
when (cc.segment3 like '4%' or cc.segment3 like '5%')
then (between to_date(:P_Start_date2,'dd/mm/rrrr') and to_date(:P_END_date,'dd/mm/rrrr'))
end) h.default_effective_date
and (case
when (cc.segment3 like '4%' or cc.segment3 like '5%')
then (h.default_effective_date)
end) between to_date('01/07/2012','dd/mm/rrrr') and to_date('31/07/2012','dd/mm/rrrr')*/
group by cc.segment3 , t.description , cc.segment1,h.currency_code;
the part between tages need to modigied -
Need help converting Excel IF statement into CASE WHEN statement for Oracle
Hi,
Hope someone can help I have tried various ways of getting this to work, to no avail!
Bascially I have some figures that are minus figures, and I need to add them together to get a movement figure, but I need to treat the minus figures as minus, if that makes sense, rather than the usual... a minus and a minus makes a plus.
For example:- Budget Figure = -1% and Actual Figure = -68% so the movement needs to be -69%.
The IF statement I have been using in Excel is the following:-
=IF(FO110<0,(FP110-(IF(FO110=0,1,FO110)*-1)),FP110-IF(FO110=0,1,FO110))
Which when using the figures as above = -69%
Cell FO = The Budget Figure
Cell FP = The Actual Figure
However, when I created the CASE statement in Oracle, the figure in the query comes back as -0.67, which is oviously not what I want to happen when both actual and budget are minus figures; however when they are a minus and a plus, it works perfectly fine.
Any help on this would be most appreciated!
Kind regards,
AnnmarieHappy I did'n mess something up :)
Nevertheless, don't show it too much around because
case when budget < 0
then actual - case when budget = 0 /* will never happen */
then 1 /* will never happen */
else budget
end * (-1) /* -budget * (-1) remains only */
else actual - case when budget = 0
then 1
else budget
end
endso at least try the following (if case is more readable as decode for you). Let's hope it works as I don't have a Database at hand
case when budget < 0
then actual + budget
else actual - case when budget = 0
then 1
else budget
end
end Regards
Etbin -
Case When contruct in a trigger
Hi gurus,
I want to use the case when construct in a trigger instead of the regular select count(*) into statement to check the existence of a row but it does not work. I have used this extensively when I generate XML from many tables in a sproc and they work beautifully. Here is my code from the trigger.
if :new.charge_conditions in ('DAB', 'DOB', 'IMB') then
/* -- do not know why the case does not work here! 12/04/2006
case
when (select count(*) from boarder_date
where booking_no = :new.booking_no) <= 0 then
insert into boarder_date(booking_no, start_date)
values(:new.booking_no, sysdate);
end ;
select count(*) into v_boarderCount from boarder_date
where booking_no = :new.booking_no;
if v_boarderCount <= 0 or v_boarderCount is null then
insert into boarder_date(booking_no, start_date)
values(:new.booking_no, sysdate);
end if;
end if;
The error message is this,
PLS-00103: Encountered the symbol "SELECT" when expecting one of
the following:
( - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
PLS-00103: Encountered the symbol ")" when expecting one of the
following:
. ( * @ % & - + ; / at for mod rem <an exponent (**)> and or
group having intersect minus order start union where connect
|| indicator
I have to use select count(*) into to make it work. Is that not allowed in a trigger or did I do something wrong with the syntax here? Thanks a lot.
benI don't believe that you can use the result of a query in a CASE statement in PL/SQL like you can in SQL. While it's perfectly valid to have
1 select case when (select count(*) from emp) > 1
2 then 1
3 else 0
4 end
5* from dual
SCOTT @ nx102 JCAVE9420> /
CASEWHEN(SELECTCOUNT(*)FROMEMP)>1THEN1ELSE0END
1
Elapsed: 00:00:00.01you cannot do the same in PL/SQL
1 declare
2 i number;
3 begin
4 i := case when (select count(*) from emp) >= 1
5 then 0
6 else 1
7 end;
8* end;
SCOTT @ nx102 JCAVE9420> /
i := case when (select count(*) from emp) >= 1
ERROR at line 4:
ORA-06550: line 4, column 19:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternativ
ORA-06550: line 4, column 43:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. , @ ; for <an identifier>
<a double-quoted delimited-identifier> group having intersect
minus order partition start subpartition union where connect
Elapsed: 00:00:00.04Of course, even if you could use a query in PL/SQL, you couldn't perform DML
1 declare
2 i number;
3 begin
4 i := case when 2 >= 1
5 then
6 begin
7 insert into emp( empno, ename ) values (17, 'JUSTIN');
8 end;
9 else 1
10 end;
11* end;
SCOTT @ nx102 JCAVE9420> /
begin
ERROR at line 6:
ORA-06550: line 6, column 15:
PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternatively-quot
ORA-06550: line 9, column 13:
PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
Elapsed: 00:00:00.07Justin -
Update multiple rows using CASE WHEN
I have the table ACCOUNT of structure as follow:
ACCOUNT_ID
ACCOUNT_STATUS
004460721
2
042056291
5
601272065
3
I need to update the three rows at once using one SELECT statement such that, the second column will be 5, 3, 2 respectively.
I used the following query but seems there is something missing
UPDATE ACCOUNT
SET ACCOUNT_STATUS = CASE
WHEN ACCOUNT_STATUS = '004460721' THEN 5
WHEN ACCOUNT_STATUS = '042056291' THEN 3
WHEN ACCOUNT_STATUS = '601272065' THEN 2
WHERE ACCOUNT_ID IN ('004460721','042056291','601272065')
My question, is this way correct? if no, can I use CASE WHEN statement and how or I only have choice of using SUB-SELECT to acheive that in one statement?Hi,
Hawk333 wrote:
I have the table ACCOUNT of structure as follow:
ACCOUNT_ID
ACCOUNT_STATUS
004460721
2
042056291
5
601272065
3
I need to update the three rows at once using one SELECT statement such that, the second column will be 5, 3, 2 respectively.
I used the following query but seems there is something missing
UPDATE ACCOUNT
SET ACCOUNT_STATUS = CASE
WHEN ACCOUNT_STATUS = '004460721' THEN 5
WHEN ACCOUNT_STATUS = '042056291' THEN 3
WHEN ACCOUNT_STATUS = '601272065' THEN 2
WHERE ACCOUNT_ID IN ('004460721','042056291','601272065')
My question, is this way correct? if no, can I use CASE WHEN statement and how or I only have choice of using SUB-SELECT to acheive that in one statement?
What happens when you try it?
Did you mean "WHEN ACCOUNT_ID = ..."?
A CASE expressions always needs an END keyword.
Depending on your requirements (that is, why are those rows being changed, and how do you determine the new values) a CASE expression in an UPDATE statement, similar to what you posted, could be a good way to do it. MERGE (instead of UPDATE) would also be an option, especially if you want to avoid updating rows that already happen to have the correct values.
Maybe you are looking for
-
Hi, I have to execute F.27 for dunning data to customers. The output is coming correct for the invoices but for Credit and other types the column is not aligned... I have tried using ",," (tab) but that does not help me ... how do I correct the outpu
-
Multiple Pages for pics?
Why is it when I click on the 4th page it doesnt show pages 5-6? I find that sometimes people dont realize they need to click on the tiny arrow. Any way to show other pages automatically?
-
I am getting an error "Workflow [b_X_MM_ATTR] not found". Can someone please help me. This workflow name is mentioned in the 3rd party tab of the Infopackage.
-
[JHS10.1.3] Date problem
Running into a date problem for a forms migration. I have a date field in the database, normal date field. The users want to be able to search on partial dates. So adding a between on this field will only generate two date selectors, which is not wha
-
Web Service Introspection + Ant
I'm working on a large project which involves pulling data from over 30 different web services. I'm using the Web Service Introspection Wizard in Flex Builder 3 to create proxy classes. I am then running my own custom ant tasks to automatically gener