How to use "DECODE"
may i know how to apply "DECODE" in SQL?
Daphne,
you can find the usage of decode here:
http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10759/functions036.htm
regards
Rainer
Similar Messages
-
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 concept in htp.p?
Hi All,
How to use decode function in htp.p syntax?
Thanks,
Anoo..Hi,
Could you please give more details on what you like to achieve? Decode function works only in sql query.
You can first do sql query, and then do htp.p.
Or you can use PL/SQL IF or CASE statements.
You can also create custom decode function if you need.
Regards,
Oleg -
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 -
How to use Decode with a variable
Maybe you've seen this done.
I want to take statement strMenu4 and subtract the previous value
that is stored in an array as shown in strMenu5.
My problem is this change as shown in strMenu5 produces
an error code:
ORA-00904 invalid column name
So either I'm coding something wrong or it can't be done within the
SUM(decode) construct. If I replace the variable with a literal
like 1000 the code works:
SUM(decode(name,'DBWR lru scans', c1.value) - 1000) DBWR_lru_scans
but with an array variable or even a constant the code fails:
SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, "
Any ideas how I can use Decode with a variable and not have it interrupt it
as a invalid column name?
Thanks,
Joseph Karpinski
[email protected]
[email protected]
strMenu4 = "SELECT d1.instance, " _
& " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
& " SUM(decode(name,'DBWR buffers scanned', c1.value)) DBWR_buffers_scanned, " _
& " SUM(decode(name,'DBWR lru scans', c1.value)) DBWR_lru_scans, " _
& " SUM(decode(name,'db block gets', c1.value)) db_block_gets , " _
& " SUM(decode(name,'consistent gets', c1.value)) consistent_gets , " _
& " SUM(decode(name,'physical reads', c1.value)) physical_reads " _
& " from dual, v$sysstat c1, v$thread d1 " _
& " group by d1.instance "
strMenu5 = "SELECT d1.instance, " _
& " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
& " SUM(decode(name,'DBWR buffers scanned', c1.value) - Array(2)) DBWR_buffers_scanned, " _
& " SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, " _
& " SUM(decode(name,'db block gets', c1.value) - Array(4)) db_block_gets , " _
& " SUM(decode(name,'consistent gets', c1.value) - Array(5)) consistent_gets , " _
& " SUM(decode(name,'physical reads', c1.value) - Array(6)) physical_reads " _
& " from dual, v$sysstat c1, v$thread d1 " _
& " group by d1.instance "Re-posting in the SQL and PL/SQL forum.
It's more appropriate.
Thanks,
Closing -
How to Use Decode for manipulating time
How can i use Decode function to manipulate time...
Example
suppose ive employee and his time in...i want to display status field by using decode,which reflects if that employee came after 09:15:00 the status='L' else this will print 'P'
i tried this
SELECT pin_code,DECODE( to_char(ACCESS_TIME,'HH24:MI:SS'>'09:15:00','l','P'))STATUS
FROM ATTENDANCE_REG
this query hasn't work...
waiting for reply
Regards
Danish HayderSQL> select case when to_number(to_char(sysdate,'sssss')) > 33300 then 'L'
2 else 'P' end status
3 from dual;
S
L
1 row selected. -
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 -
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 -
How to use Decode Function in Webi / Designer - BOE XI 3.1
Hi All,
I have a SQL query which needs to include in the webi report.
Below is the query :
SELECT
SECURITY.SEC_CUSIP_NO "CUSIP",
SECURITY.STY_SEC_TY_CD "SECURITY TYPE",
SECURITY.SEC_DERIVED_DESC_TX "SECURITY DESCRIPTION",
SECURITY.sec_dep_teleg_de "FED DESCRIPTION",
SEC_STND_PR "STANDARD/FACTORED PRICE",
SEC_STND_PR_EFF_DT "STANDARD/FACTORED PRICE Date",
SECURITY.SEC_YIELD_PR "YIELD PRICE",
SECURITY.SEC_YIELD_PR_DT "YIELD RICE EFFECTIVE DATE",
STND_PR_VND_VENDOR_CD "PRICE SOURCE",
SEC_MATURITY_DT "MATURITY DATE",
SEC_ISSUE_DT "ISSUE DATE",
CASE WHEN SECURITY.STY_SEC_TY_CD IN ('BA','CD','CDD','CDE','CDM','CDV','CP','CPD') THEN
(position.PSN_AVAIL_PAR_VL + position.PSN_COLLAT_PAR_VL) * (SECURITY.SEC_STND_PR) / 100
ELSE
(position.PSN_AVAIL_PAR_VL + position.PSN_COLLAT_PAR_VL) * ( ( SECURITY.SEC_YIELD_PR * DECODE (SECURITY.SEC_PRIN_FT, 0, 1, SECURITY.SEC_PRIN_FT))) / 100
* DECODE(SECURITY.STY_SEC_TY_CD, 'AMP', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 'MMP', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 'AMPT', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 1 )
END
) "MARKET VALUE"
FROM SECURITY SECURITY
, position position
WHERE STND_PR_VND_VENDOR_CD = 'VT'
AND SEC_MATURITY_DT > SYSDATE-1
and SECURITY.sec_sys_id=position.sec_sys_id
I could create a report which has selected objects and defined where condition from the above query. I have also created a variable for 'when' & 'then' condition but stuck at else part which contains 'DECODE' function.
Please suggestions how to write the same at the report or designer level.
Thanks,
Rameez ShaikhHi Rameez,
Looking at the query you can create the object directly in the universe, either create two objects one for inner decode and use it in outer case logic. In report it is nothing but nested if logic. For eg. If(a=b;1;(if(a=c;2;3))
Thanks
Gaurav -
How to use decode on the value part ?
i have a query
select
col1, col2, col3
from
tab1, tab2
where
cond1= 'parameter1'
I need to modify this query to use a decode function instead of this parameter1.
something like
select
col1, col2, col3
from
tab1, tab2
where
cond1= decode(parameter1, 'x','val1','val2')
How can i use such a decode function on the value part ?
IF i try this, it gives me no data found error....
The following is also not working..IS there a way to do such cases ?
select
col1, col2, col3
from
tab1, tab2
where
cond1= ( select decode(parameter1, 'x','val1','val2') from dual )Of course it works...
test@ORA10G>
test@ORA10G> drop table table1;
Table dropped.
test@ORA10G> create table table1 (
2 name varchar2(50),
3 condition1 varchar2(2),
4 condition2 varchar2(10)
5 );
Table created.
test@ORA10G> insert into table1 (name,condition1,condition2)
2 values ('Hi there!!','xx','VAL1');
1 row created.
test@ORA10G> commit;
Commit complete.
test@ORA10G>
test@ORA10G> select * from table1;
NAME CO CONDITION2
Hi there!! xx VAL1
1 row selected.
test@ORA10G>
test@ORA10G> declare
2 CodeL varchar2(3) := 'AB';
3 CodeName varchar2(50) := '';
4 begin
5 select decode(CodeL,'AB','VAL1', CodeL)
6 into CodeName
7 from dual;
8 dbms_output.put_line('CodeName: '||CodeName);
9 select Name
10 into CodeName
11 from table1
12 where condition1='xx'
13 and condition2 in ( select decode(CodeL,'AB','VAL1', CodeL) from dual );
14 dbms_output.put_line('CodeName: '||CodeName);
15 end;
16 /
CodeName: VAL1
CodeName: Hi there!!
PL/SQL procedure successfully completed.
test@ORA10G>pratz -
How to use decode for a not = expression ?
Hi ,
i want to use in my seelct query decode( val1 , IF not = 'abc' , sum(val1)
how can i express it as a not equal or even a >= in a decode ?
kindly advise
tks & rdgshi ,
i have used a function to resolve my issue
tks for all the info
rdgsYou would be better doing it all in SQL for performance reasons so you do not context switch between PL/SQL and SQL all the time...
Ok, without using CASE...
SQL> create table a (stupid_number_in_varchar VARCHAR2(10));
Table created.
SQL> insert into a
2 select '3' as stupid_number_in_varchar from dual union all
3 select 'X' from dual union all
4 select '4' from dual union all
5 select 'X' from dual union all
6 select '5' from dual;
5 rows created.
-- Strip out non numeric values...
SQL> ed
Wrote file afiedt.buf
1 select decode(stupid_number_in_varchar, 'X', null, stupid_number_in_varchar) as mynum
2* from a
SQL> /
MYNUM
3
4
5
-- Only take numbers >= 4 ...
SQL> ed
Wrote file afiedt.buf
1 select decode(sign(mynum-4), -1, null, mynum)
2 from (
3 select decode(stupid_number_in_varchar, 'X', null, stupid_number_in_varchar) as mynum
4 from a
5* )
SQL> /
DECODE(SIG
4
5
-- Sum the result...
SQL> ed
Wrote file afiedt.buf
1 select sum(mynum) from
2 (
3 select decode(sign(mynum-4), -1, null, mynum) mynum
4 from (
5 select decode(stupid_number_in_varchar, 'X', null, stupid_number_in_varchar) as mynum
6 from a
7 )
8* )
SQL> /
SUM(MYNUM)
9
SQL> -
Hi All
I have one table called agreementproductkey and some columns namely Agreementproductid, keytext, fliename etc.
There is some condition if agreement product id in not null and keytext field and fileaname field is null then i need to display the value as "NO FILE"
This what i tried
select decode(keytext || ' | ' || filename,null,'No File',keytext || filename) display_value, id return_value
from agreementproductkey
where agreementproductid = 3173
But i didnt get the NO FILE when key text field and filename field is null for this agreementproductid.
Thanks & Regards
Srikkanth.MHi Srikkanth,
In the decode part, you have concatenated keytext and filename using '|' due to which it will never evaluate to null, and hence 'NOFILE' will never be displayed. CASE is a better option instead of DECODE.
SQL> CREATE TABLE t (agreementid NUMBER, keytext VARCHAR2(10), filename VARCHAR2(10));
Table created
SQL> INSERT INTO t VALUES (123, 'ABC', 'ABCFile');
1 row inserted
SQL> INSERT INTO t VALUES (123, NULL, 'XYZFile');
1 row inserted
SQL> INSERT INTO t VALUES (123, 'PQR', NULL);
1 row inserted
SQL> INSERT INTO t VALUES (123, NULL, NULL);
1 row inserted
SQL> SELECT agreementid, keytext, filename,
2 DECODE(agreementid, NULL, 'AGREEMENT NULL', DECODE(keytext || filename, NULL, 'NO FILE', 'FILE PRESENT'))
3 FROM t;
AGREEMENTID KEYTEXT FILENAME DECODE(AGREEMENTID,NULL,'AGREE
123 ABC ABCFile FILE PRESENT
123 XYZFile FILE PRESENT
123 PQR FILE PRESENT
123 NO FILE
SQL> Regards
Ameya -
How to use decode or case function in this?
Hi,
I want to implement this condition in a query without using any user defined functions
1.if (T_fees = 'ABC' then if (p_fees>0) then nvl(p_fees,0) else (p_fees + a_fees) else 0)
2. if(g_fees <> (if t_fees = 'ABC' then if (p_fees>0) then nvl(p_fees,0) else (p_fees)
else 0
Is it possible to use any nested 'CASE' statement ?
This is very urgent...
Regards,
ShilpaHi,
Is it possible to use any nested 'CASE' statement ?Yes it it possible.
"afiedt.buf" 7 lines, 89 characters
1 SELECT sysdate FROM DUAL
2 WHERE 1 = (CASE WHEN 1=1 AND 2=2 THEN
3 1
4 ELSE
5 2
6* END)
SQL>/
SYSDATE
07-AUG-06
1 row selected.
"afiedt.buf" 11 lines, 139 characters
1 SELECT sysdate FROM DUAL
2 WHERE 1 = (CASE WHEN 1=1 AND 2=2 THEN
3 CASE WHEN 3=3 AND 4=4 THEN
4 1
5 ELSE
6 0
7 END
8 ELSE
9 0
10* END)
SQL>/
SYSDATE
07-AUG-06
1 row selected.
You can implement your logic.
Regards -
How to Use Decode with Add_months
Here is my code
DECLARE
no_of_rents number(3) :=0;
BEGIN
select (:period_mm/decode(:pay_freq,'M',1,'Q',3,'H',6,12))
into no_of_rents
from dual;
for i in 1..no_of_rents loop
insert into mk_rnt
(MK_NO,RENT_NO,DUE_DATE,DUE_AMT)
values(:mk_cnt.mk_no,i,ADD_MONTHS(:FIRST_RENT_DT,i-1),:FIRST_RENT);
end loop;
end;
COMMIT;
My Requirment
In the field due_date, the next date will be according to Pay_frequency
Suppose if Pay_freq=M then next due_date will be i+1 of last Date,
if pay_freq='Q' then due_date will be i+3 of last date and so onDECLARE
no_of_rents number(3) :=0;
rents number :=0;
rt_no number(3) :=1;
mcr varchar2(2) ;
BEGIN
select (:period_mm/decode(:pay_freq,'M',1,'Q',3,'H',6,12))
into no_of_rents
from dual;
for i in 1..no_of_rents loop
if :pay_freq='M'
THEN MCR:=1;
ELSIF :pay_freq='Q'
THEN mcr:=3;
elsif :pay_freq='H'
THen mcr:=6;
else mcr:=12;
end if;
RT_NO:= MCR*(I- 1);
insert into mk_rnt
(MK_NO,RENT_NO,DUE_DATE,DUE_AMT)
Values(:mk_cnt.mk_no,i,ADD_MONTHS(:FIRST_RENT_DT,RT_NO),:FIRST_RENT);
end LOOP;
end;
COMMIT; -
How to use CASE or DECODE in this?
Hi , I have a query like this
AND ...smthng
AND --smtg
AND age BETWEEN ....For the last AND clause for age column, I have the values passed from IN param of this SP in form of strings
'0-30', '31-60', '61-90', and '91+'. The IN param is of course VARCHAR2.
What I want to do in this AND clause
e.g. for age range '0-30', the AND clause should be
AND age BETWEEN ( if this range is '0-30' then ) 0 AND 30 ( if range is '31-60') then BETWEEN 31 AND 60.. and so on...
However this BETWEEN will work till 61-90 range but for 91+ , there is no upper bound so it should be
AND age>91
Now , i am not sure how to achieve this both BETWEEN and '>' clauses in the same AND statement.
DECODE will not work and am not sure how to use the CASE in this situation as even that can not solve this issue.
Dont want to make the last AND clause dynamic
Please suggest me how to do this.
Thanks,
Aashish
Edited by: Aashish S. on Oct 21, 2011 6:01 PMA third alternative would be to choose a suitably large value as a high end for the 91+ group and use something like:
age between to_number(CASE WHEN instr(:param, '+') = 0
THEN substr(:param, 1, instr(:param, '-') -1)
ELSE substr(:param, 1, instr(:param, '+') -1) end) and
to_number(CASE WHEN instr(:param, '+') = 0
THEN substr(:param, instr(:param, '-') +1)
ELSE '9999' END)John
Maybe you are looking for
-
Install Boot Camp on 3rd drive (3TB)?
I just installed a new 3TB drive in my early 2008 Mac Pro, which is runing ML. I would like to use Boot Camp to install Windows 7 on a 300 GB partition on that new drive and to have it bootable. I would like to retain the rest of the drive for use
-
Adobe Flash updater keeps crashing
Safari (8.0.4) is saying the Adobe Flash player is out of date. I have downloaded the updater and tried to install but the installer itself keeps crashing. I tried dowloading again with the same result. Tried on a completely different computer ,same
-
My program is win32 EXE written in C++. It calls java method through JNI. Now I must create and start new JVM each time the exe is started. Is it possible to find and join JVM that is already running?
-
Audigy 2 ZS questions.. need help ple
am trying to buy creative audigy 2 SZ but i have few questions before i do - i want to disable the 48Hz and set it to 44Hz for the sound.. will it effect the performance for EAX 4.0 for games? 2- do i need to connect the card to my power supply? 3- i
-
Help with Gmail IMAP in Mac Mall: Folders transfered from Exchange server
I'm in the process of migrating a mail account from an Exchange server over to Goggle Apps. I've transfered all old mail and the existing Exchange folder structure over to Gmail. Gmail doesn't recognize folders but instead reads the folder structure