Using DECODE
I am having proplems using decode. I need to take values from on feild and decode them to a yes or no in another. If the value is 1 then = yes, if null or 0 then no.
the feild that i am decoding has 5 values like 11111 would I use decode in a post-record trigger
DECODE(substr(:sites,1) '1', 'Yes', 'null','No'...)
Thanks
Sean
null
Hi Sean,
The previous answer will work if there was only one row to be created. If you need 5 rows then do it this way. In the post-insert trigger of the sites block :
if :sites like '1%' then
insert into table_name (Process Order,
Instance,Description,Replicated)
values(:PROCESS_ORDER, :INSTANCE, :DESCRIPTION, 'd09 ATLANTIC YES');
else
insert into table_name (Process Order,
Instance,Description,Replicated)
values(:PROCESS_ORDER, :INSTANCE, :DESCRIPTION, 'd09 ATLANTIC NO');
end if;
You do this for each of the digit of the sites field. for instance for the second digit :
If :sites like '_1%' then
insert into table_name (Process Order,
Instance,Description,Replicated)
values(:PROCESS_ORDER, :INSTANCE, :DESCRIPTION, 'd09 PACIFIC YES');
else
insert into table_name (Process Order,
Instance,Description,Replicated)
values(:PROCESS_ORDER, :INSTANCE, :DESCRIPTION, 'd09 PACIFIC NO');
end if;
And so on ... At the end be sure to commit the form. then when you hit the explain button and go the the other block, have execute_query in the code so that the query is executed and it brings up these 5 rows. Be sure to link the 2 blocks with a column.
If you could email me your form I can explain it better. My email is [email protected]
Hope you could make sense of what I wrote.
Sudha
Similar Messages
-
I need to know if there is any way to use a range of values from
database and decode to certain text. I am able to do with one
value.
for example:
DECODE(column_name,'216767111','Unlimited',column_name)
above argument works with one value only. How about a range,
ex: 216767000 to 216767111. I need to use only SQL. No PL/SQL.
Kinldly need some body's help
Thanks
MunisWhich version of the database? If it's 8i+ then you can use
the CASE function
for example:
(CASE WHEN column_name between 216767000 and 216767111
THEN 'Unlimited' ELSE column_name END)
This won't work in PL/SQL because they're introducing a CASE
statement does soemthing different.
rgds, APCHello Andrew
Thank you for response. I am using 8i. 8.1.6. However using
CASE, I get inconsistent data type, ORA-00932: inconsistent
datatypes. I able to work it out with other response using
DECODE(sign(. Do you have any idea why i am getting this error.
If time permits, let me know -
How to use decode function in oracle apex 3.2.1
Hello Friends,
how to use decode
var_decode := 'decode'||'('|| var1 ||','|| -800000000000000||','||'.A'||','||
appreciate your help.
regards/kumar
Edited by: kumar73 on Apr 23, 2010 12:25 PM
Edited by: kumar73 on Apr 23, 2010 12:27 PMHey just on this particular point, there's a handy PL/SQL method for escaping quotation characters which may reduce the complexity of the string.
For example, if I had a string:
I can't understand why quoting my 'quote' characters never seems to "work"!and I wanted to pass that into a variable, I could do this:
vc_foo:= 'I can''t understand why my ''quote'' characters never seems to "work"!';or I could use the q function, whereby you enclose your string within a quote delimiter of your choice, like so:
vc_foo:= q'^I can't understand why quoting my 'quote' characters never seems to "work"!^';
note: I used the caret (^) character by convention but any valid character will do
...all of which leaves your original string more or less unadulterated.
I find this particularly useful for generating dynamic SQL and dynamic PL/SQL, as it leave the query looking a tad more readable, although if you are chaining together multiple strings with double-pipes, it can make it a little muddier. I find it particularly useful if I'm going to deal with string data from, for example, a field with Irish-derived surnames such as "O'Reilly"...
Give it a go! -
How to use DECODE function in Exspression?
Hi,
Can we use DECODE in Expression?
I'm trying to use DECODE function but there is an error during the validation. But when i validate the mapping, it is successfully compiled but it is failed during deployment.
But if I use CASE instead of DECODE, it works fine.
Can we use DECODE in OWB???
Thanks
RajHi,
In OWB 10gR2, if your are using only one DECODE in an expression, it's working. The package will compile when deploying the mapping. OWB will replace the DECODE by a CASE.
But when you are using nested decode in an expression ( for example : decode(col1, 1, 'M', decode(col2, 'Madame', 'Mme', null)) ) only the first one is replaced by a case at deployment.
In ROW_BASED mode, text of the expression is used outside of an sql statement and deployment will fails with "PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only."
If operating mode for the mapping is set to SET_BASED, it's working because the expression is used in an sql statement.
I have logged a SR in metalink for this issue and a bug is opened (bug 5414112).
But I agree with you, it's better to use case statement.
Bernard -
Hi all
We can use decode function in the Select columns as well as Group by Columns as well as Having Clause
Can we use decode function in the where clause also. If yes can u give small sample
Suresh Bansal""DECODE in the WHERE clause"
http://psoug.org/reference/decode_case.html
WHERE empid = DECODE(posn,
0, st.areadir,
1, st.areamgr,
2, NVL(st.storemgr1, st.storemgr2),
3, NVL(st.asstmgr1, NVL(st.asstmgr2,
st.asstmgr3))) -
How can we use DECODE function in where clause.
Hi Guys,
I have to use DECODE function in where clause.
like below
select * from tab1,tab2
where a.tab1 = b.tab2
and decode(code, 'a','approved')
in this manner its not accepting?
Can any one help me on this or any other aproach?
Thanks
-LKR>
I am looking for to decode the actual db value something in different for my report.
like if A then Accepted
elseif R then Rejected
elseif D then Denied
these conditions I have to check in where clause.
>
what are you trying to do?
may be you are looking for
select * from tab1,tab2
where a.tab1 = b.tab2
and
(decode(:code, 'A','Accepted') = <table_column>
or
decode(:code, 'R','Rejected') = <table_column>
or
decode(:code, 'D','Denied') = <table_column>
) -
Query to find out sum by using decode or case
Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
INSNAME INSID STATUS
AAA 1000 Scheduled
AAA 1000 Processing
BBB 1001 Inspector
BBB 1001 Scheduled
BBB 1001 Processing
BBB 1001 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
CCC 1002 Scheduled
CCC 1002 Processing
CCC 1002 Inspector
Required Output...
INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
AAA 1000 1 1 0
BBB 1001 1 1 2
CCC 1002 2 2 2And if you want it with CASE statement:
WITH test_table AS
SELECT 'AAA' insname, 1000 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'AAA' insname, 1000 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'BBB' insname, 1001 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Scheduled' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Processing' status FROM DUAL UNION ALL
SELECT 'CCC' insname, 1002 insid, 'Inspector' status FROM DUAL
SELECT insname
,insid
,SUM(CASE WHEN status = 'Scheduled'
THEN 1
ELSE 0
END
) sum_of_scheduled
,SUM(CASE WHEN status = 'Processing'
THEN 1
ELSE 0
END
) sum_of_processing
,SUM(CASE WHEN status = 'Inspector'
THEN 1
ELSE 0
END
) sum_of_inspector
FROM test_table
GROUP BY insname
,insid;
Regards
Arun -
Using decode function without negative values
Hi friends
I am using oracle 11g
I have at doubt regarding the following.
create table Device(Did char(20),Dname char(20),Datetime char(40),Val char(20));
insert into Device values('1','ABC','06/13/2012 18:00','400');
insert into Device values('1','abc','06/13/2012 18:05','600');
insert into Device values('1','abc','06/13/2012 18:55','600');
insert into Device values('1','abc','06/13/2012 19:00','-32768');
insert into Device values('1','abc','06/13/2012 19:05','800');
insert into Device values('1','abc','06/13/2012 19:10','600');
insert into Device values('1','abc','06/13/2012 19:15','900');
insert into Device values('1','abc','06/13/2012 19:55','1100');
insert into Device values('1','abc','06/13/2012 20:00','-32768');
insert into Device values('1','abc','06/13/2012 20:05','-32768');
Like this I am inserting data into table for every 5 minutes Here i need the result like
output:
Dname 18:00 19:00 20:00
abc 400 -32768 -32768
to retrieve this result i am using decode function
SELECT Dname,
MAX(DECODE ( rn , 1,val )) h1,
MAX(DECODE ( rn , 2, val )) h2,
FROM
(SELECT Dname,Datetime,row_number() OVER
(partition by Dname order by datetime asc) rn FROM Device
where substr(datetime,15,2)='00' group by Dname.
According to above data expected result is
Dname 18:00 19:00 20:00
abc 400 600(or)800 1100
This means I dont want to display negative values instead of that values i want to show previous or next value.
Edited by: 913672 on Jul 2, 2012 3:44 AMAre you looking for something like this?
select * from
select dname,
datetime,
val,
lag(val) over (partition by upper(dname) order by datetime) prev_val,
lead(val) over (partition by upper(dname) order by datetime) next_val,
case when nvl(val,0)<0 and lag(val) over (partition by upper(dname) order by datetime) >0 then
lag(val) over (partition by upper(dname) order by datetime)
else
lead(val) over (partition by upper(dname) order by datetime)
end gt0_val
from device
order by datetime
where substr(datetime,15,2)='00';Please take a look at the result_column gt0_val.
Edited by: hm on 02.07.2012 04:06 -
SQL Cost of using DECODE built-in
I'm trying to tune some SQL and wondering about the cost of using DECODE. I believe that by using this function, it prevents any index on the related column from being used.
Do any "Tuning Gurus" have opinions on the use of DECODE?
TIA,As a general rule, the Oracle built-in functions are pretty fast to execute. You will not really see any difference in performance between a sql statement using a built-in function in the SELECT clause, and one not using one.
However, a lot depends on where and how you use the DECODE.
SELECT col1,DECODE(col2,1,'YES',2,'NO',3,'MAYBE')
FROM table
will be no slower than
SELECT col1,col2
FROM table
If there is an index on col2, then
SELECT col1,DECODE(col2,1,'YES',2,'NO',3,'MAYBE')
FROM table
WHERE col2 BETWEEN 1 AND 3
will use it. However
SELECT col1,col2
FROM table
WHERE DECODE(col2,1,'YES',2,'NO',3,'MAYBE') IN ('YES','NO','MAYBE')
will not (unless you have a function based index). But,
SELECT col1,col3
FROM table
WHERE col2 BETWEEN 1 and 3 and
col3 = DECODE(col2,1,'YES',2,'NO',3,'MAYBE') IN ('YES','NO','MAYBE')
will.Note that "will" in the above really means can. it is up to the optimizer whether it actually is used or not.
HTH
John -
How to use decode to calculate sum for different date range
I'm stuck with decode() function:
I have a table like this:
(project_id, approve_date, value, builder_code)
I want to write a SQL query to get sum of values for different month of the approve_date, and group by builder_code)
The result is like this:
builder_code Sum(value)_Sep-03 Sum(value)_Oct-03 Sum(value)_Nov03
1001 1,299 1,322 990
1002 3,332 1,222 333
I tried to use decode for this question but could not get the answer.
Thanks a lotI don't think you need a DECODE() here. I'd do something like this-
create table builder (
project_id number,
approve_date date,
value number,
builder_code number
insert into builder values( 1, to_date('09-01-2003', 'MM-DD-YYYY'), 100, 990 )
insert into builder values( 2, to_date('09-03-2003', 'MM-DD-YYYY'), 150, 990 )
insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 990 )
insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 333)
SELECT sept.builder_code, sept.sept_sum, oct.oct_sum
FROM (SELECT builder_code, sum(value) sept_sum
FROM builder
WHERE approve_date >= to_date('09-01-2003','MM-DD-YYYY')
AND approve_date < to_date('10-01-2003','MM-DD-YYYY')
GROUP BY builder_code) sept,
(SELECT builder_code, sum(value) oct_sum
FROM builder
WHERE approve_date >= to_date('10-01-2003','MM-DD-YYYY')
AND approve_date < to_date('11-01-2003','MM-DD-YYYY')
GROUP BY builder_code) oct
WHERE oct.builder_code(+) = sept.builder_code
BUILDER_CODE SEPT_SUM OCT_SUM
333 250
990 500Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com/askDDBC -
Hello,
i have question regarding PL/SQL using DECODE when i use following qurey in SQL plus it is working fine but when i try to run in tode it only display value of g.
everything else is blank........
so can u plz assist me on that .....
thanks in advance!!!!!!!!!!
SELECT
b.g,b.s,
COUNT(DECODE(a.u,'R',a.rep)) AS R,
COUNT(DECODE(a.u,'u',a.rep)) AS U,
COUNT(DECODE(a.u,'C',a.rep))AS C,
COUNT(DECODE(a.u,'I',a.rep)) AS I,
COUNT(DECODE(a.u,NULL,a.rep)) AS B
FROM AREA a, data b
WHERE a.rep = '454637'
AND a.rep = b.emp
GROUP BY b.g,b.sVery strange.
Find out if there are alterations to your SQL*PLUS envirionment. Warren mentioned user/database wierdness; the ALTER SCHEMA and other commands can affect privilges and what objects you can see. Sometimes these commands are buried in LOGIN.SQL and GLOGIN.SQL files (for SQL*PLUS), and more hard to find they can call files buried on a network somewhere.
Try the query without the DECODE() and see what's in the table without trying to modify the values in both envirionments. -
Hello Friends,
I have a query that has different columns and I am not sure what the data type of each column is ...
I want to use decode function which displays the following if the value of the column is like this ..
if the value of column is -714E ie -700000000000000 then display as .A
if the value of column is -814E ie -800000000000000 then display as .B
NOTE ; don't know which column is having the value - and i don't know the data type of the column is ..
got to use the decode function for all the columns selected ..
I want to use decode function pls let me know how to write for this kind of requirement.
appreciate your help in this rgds.
thanks/kumarDear Sir / Madam,
Thanks for your understanding.
Right now I am facing a unique problem.
I am using decode function in a select statement. The columns are dynamic some columns are of type number and some are varchar datatypes. I want to apply decode function for every column irrespective of datatype.
What i am finding difficult is if the column is a number datatype , the decode funciton is working good but if the column datatype is varchar or char datatype then i am getting ..
here's the decode function..
decode (CHAI.EVNDRIVE,
-800000000000000,'.A',
-700000000000000, '.B',
-600000000000000 ,'.C',
-500000000000000 , '.D',
-400000000000000 , '.E',
-300000000000000 , '.F',
-200000000000000, '.G',
-100000000000000 , '.H',
-1000000000, '.R' ,CHAI.EVNDRIVE ) EVNDRIVE
report error:
ORA-01722: invalid number
pls let me know how to over come this kind of scenario.
is their a way to find whether the column datatype is number or char , so that i can check the column datatype before the decode funciton is applied ..
thanks ..
kumar -
Using Decode in where clause in free hand sql
Hi,
I want to use decode in free hand sql.
for eg : this is a where condition
Outlet_Lookup.City = @variable('Enter City')
Requirement :
if we put the user prompt as "Enter City and % for all cities"
and the user enters % then the data display should be all cities or else it shld be the specific city entered in the prmpt..
Can we do that using Decode statement,
I have tried
Outlet_Lookup.City = Decode(@variable('Enter City'),'%',Outlet_Lookup.City,(@variable('Enter City')))
This is not working....
Pls guide on the same..
Thanks.Hi Mathieu,
Thanks for the solution,
I tried the following
((Outlet_Lookup.City = @variable('Enter City or % for All') or @variable('Enter City or % for All')='%'))
This is working fine....
Thanks
Regards,
Aparna. -
Using 'DECODE' instead of 'CASE' in select
Hi,
I am using CASE statements in my Select clause using 9i.
SELECT
CASE
WHEN S.cmpy_num = S.cpty_borg_num THEN
''OURS''
ELSE
''THEIRS''
END AS SDIOwner,
S.active_flag,
from SDI sI wud like to try this in 8I but 8i doesnt support CASE statements
in Select clauses.
It was recommended to use Decode in place of CASE in 8I
How can i write the decode statement for the above CASE statement in 9IGOT IT RUNNIG.THANKS A LOT JAMES...
I AM TESTING SYSTEMS ON 9I AND 8I... -
OWB Error for using Decode in Expression
Debug code deployment messages:
LINE 4558 ,COLUMN 71:
PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only
LINE 4558 ,COLUMN 17:
PL/SQL: Statement ignored
End debug code deployment messages
DBG1012: Debug deployment errors, can't run debug code.What i have experience is that if you use Decode inside the expression the OWB cannot validate it .
But it will execute perfect.
Use Case statement so that you can validate it and debug then
Cheers
Nawneet -
when i using decode in queries written for collection i.e
wwv_flow_item.select_list_from_query_xl(13,'','select decode(id,123,''XYZ'',name) from mytable ','','NO') Field1,
I get the error
report error:
ORA-06550: line 1, column 120:
PL/SQL: ORA-00913: too many values
ORA-06550: line 1, column 7:
PL/SQL: SQL Statement ignored
in the front end ?
Please let me know whether we can use decode in queries wrriten in collections if so how
Thanks in Advancewhen i using decode in queries written for collection i.e
wwv_flow_item.select_list_from_query_xl(13,'','select decode(id,123,''XYZ'',name) from mytable ','','NO') Field1,
I get the error
report error:
ORA-06550: line 1, column 120:
PL/SQL: ORA-00913: too many values
ORA-06550: line 1, column 7:
PL/SQL: SQL Statement ignored
in the front end ?
Please let me know whether we can use decode in queries wrriten in collections if so how
Thanks in Advance
Maybe you are looking for
-
How do I set up multiple users on itunes.
My two kids have my old phones and I want to set up apps and music for them and keep separate from mine...they are too young to have their own itunes account. Can anyone point me in the right direction
-
Hello all, I am using the following FM for DMS storage of documents. BAPI_DOCUMENT_CREATE2 When i execute the BAPI i get the following error : Error while checking in and storing: D:\DOCUMENTS AND SETTINGS Can anyone help out on the same and tell the
-
Well, I installed it 2 days ago and yesterday when I tried to print a packing slip and a label for Amazon customers it printed so many strange symbols that were nothing like what they are supposed to be, not even close. When I printed it with another
-
How to handle interactive list in alv reports
hi experts. how to handle interactive list in alv reports. regards. subhasis
-
The 2 messages below keep coming up when trying to open Aperture. I have recently bought another drive to do a backup to. Currently Aperture library is on both external drives on the network. One says 46G for a sec then shows 5.4mb ....??? The other