Prob with decode function
hi iam having prob with the following decode function
declare
c varchar2(20);
begin
select decode(deptno,
10, 'accounting',
20,'Research',
30 ,'sales',
40,'operations','UNKNOWN') into c from dept where loc='DALLAS';
dbms_output.put_line('DEPARTMENT in DALLAS'||c);
select decode(deptno,
10, 'accounting',
20,'Research',
30 ,'sales',
40,'operations','UNKNOWN') into c from dept where loc='INDIA';
dbms_output.put_line('DEPARTMENT IN INDIA'||c);
end;
iam getting no_data_found exception which is reasonable..but what happened to 'unknown' clause in decode function.
thank u
rajiv
Please see the responses to your prob in decode function
John
Similar Messages
-
Error while replacing IF statements with DECODE function in procedure
Hi All,
I have created a procedure which has nested IF statements. Now I want to replace the IF statements with DECODE functions to improve performance.
Procedure:
IF (var_int_sev = '0')
THEN
var_sev := '2';
ELSE
SELECT sev
INTO var_int_sev
FROM errorconfig
WHERE errorcode = var_errorcode;
var_sev := var_int_sev;
END IF;
I converted the above IF statement into DECODE function as mentioned below:
var_Sev := DECODE(var_int_sev,0,2,SELECT severity FROM errorconfig WHERE errorcode=var_ErrorCode)
But it throws below error at the select statement used inside DECODE.
Error(58,51): 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
Can someone help me in converting the IF to DECODE in the above case. Also how can we use a select statement inside decode.instead of trying to rewrite all your code and hoping that the performance will be better, it's a better option to investigate and find out which part of your application is slow
read this:
When your query takes too long ... -
OWB3i - Validate with DECODE function
Hi,
in OWB 3i when I validate an expression defined inside an expression operator with DECODE() function (which is not include in default transformation) come this error:
Line 1, Col 1:
PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only
but it seems only a warning because when i generate the scripts and run no other error happen and all works fine.
RegardsHi,
Welcome to the forum!
When you use a default value, the last argument to DECODE is the actual value you want as a default.
For example:
SELECT ename
, deptno
, DECODE ( deptno
, 30 , 'Sales'
, 'All others' -- Default value
) AS dname
FROM scott.emp
ORDER BY ename
;Output:
ENAME DEPTNO DNAME
ADAMS 20 All others
ALLEN 30 Sales
BLAKE 30 Sales
CLARK 10 All others
FORD 20 All others
JAMES 30 Sales
JONES 20 All others
KING 10 All others
MARTIN 30 Sales
MILLER 10 All others
SCOTT 20 All others
SMITH 20 All others
TURNER 30 Sales
WARD 30 Sales
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
If you can show the problem using commonly available tables (such as those in the scott schema) then you don't need to post any sample data; just the results and the explanation.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
hi iam having prob with the following decode function
declare
c varchar2(20);
begin
select decode(deptno,
10, 'accounting',
20,'Research',
30 ,'sales',
40,'operations','UNKNOWN') into c from dept where loc='DALLAS';
dbms_output.put_line('DEPARTMENT in DALLAS'||c);
select decode(deptno,
10, 'accounting',
20,'Research',
30 ,'sales',
40,'operations','UNKNOWN') into c from dept where loc='INDIA';
dbms_output.put_line('DEPARTMENT IN INDIA'||c);
end;
iam getting no_data_found exception which is reasonable..but what happened to 'unknown' clause in decode function.
thank u
rajivDECODE only works on rows returned from the database. If there are no rows, DECODE has nothing to work on.
Consider:
SQL> SELECT DECODE(dummy, 'X', 'Found a row', 'No row')
2 FROM dual;
DECODE(DUMM
Found a row
SQL> SELECT DECODE(dummy, 'X', 'Found a row', 'No row')
2 FROM dual
3 WHERE 1=2;
no rows selectedIf you want to have c contain UNKNOWN when there are no rows returned, you need to do something more like:
DECLARE
c VARCHAR2(20);
BEGIN
BEGIN
SELECT DECODE(deptno, 10, 'accounting',
20,'Research',
30 ,'sales',
40,'operations','UNKNOWN')
INTO c
FROM dept
WHERE loc='DALLAS';
EXCEPTION
c := 'UNKNOWN';
END;
DBMS_OUTPUT.Put_Line('DEPARTMENT in DALLAS'||c);
BEGIN
SELECT DECODE(deptno, 10, 'accounting',
20,'Research',
30 ,'sales',
40,'operations','UNKNOWN')
INTO c
FROM dept
WHERE loc='INDIA';
EXCEPTION
c := 'UNKNOWN';
END;
DBMS_OUTPUT.Put_Line('DEPARTMENT IN INDIA'||c);
END;HTH
John -
How to convert rows into columns with decode function
Hi,
How to convert rows into columns with the help of decode function in oracle.
thanks and regards
P Prakashsay
col1 col2
1 10
2 20
3 30
then use
select col1,
sum(decode(col2,10,10)) "new1"
sum(decode(col2,20,20))"new2"
sum(decode(col2,30,30))"new3"
from table_name
group by col1;
we used sum u can use ny function if wont u have to give the column name i.e col2 name also
so i think u got it nw
regards -
Problem with decode function.
Hi,
Can anyone of you help me out in solving this?
It is like i wish to give different select statements according to the value of a parameter entered by user USING DECODE FUNCTION.The select statement contains some other select statements inside it.So when i execute it,it is giving error like 'ORA-00913-too many values(even when i enclose select statements within brackets).ORA-00913 too many values
Cause: The SQL statement requires two sets of values equal in number. This error occurs when the second set contains more items than the first set. For example, the subquery in a WHERE or HAVING clause may return too many columns, or a VALUES or SELECT clause may return more columns than are listed in the INSERT.
Action: Check the number of items in each set and change the SQL statement to make them equal.
the above is from oracle documentation. the brackets is not the problem, u must be using multiple items in the integrated decode queries. If the problem still exists post ur DML for further analysis.
zaibi. -
Need help with DECODE function
Hello,
I am trying to use default within the decode function and every time I get a missing expression. I have searched everywhere and cant figure out what I'm doing wrong. Thanks
select decode (request_id,0,'No files found', DEFAULT)Hi,
Welcome to the forum!
When you use a default value, the last argument to DECODE is the actual value you want as a default.
For example:
SELECT ename
, deptno
, DECODE ( deptno
, 30 , 'Sales'
, 'All others' -- Default value
) AS dname
FROM scott.emp
ORDER BY ename
;Output:
ENAME DEPTNO DNAME
ADAMS 20 All others
ALLEN 30 Sales
BLAKE 30 Sales
CLARK 10 All others
FORD 20 All others
JAMES 30 Sales
JONES 20 All others
KING 10 All others
MARTIN 30 Sales
MILLER 10 All others
SCOTT 20 All others
SMITH 20 All others
TURNER 30 Sales
WARD 30 Sales
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
If you can show the problem using commonly available tables (such as those in the scott schema) then you don't need to post any sample data; just the results and the explanation.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
Between clause with DECODE function.
Hi All,
I have one table which has object, date and quantity columns, Now based on date range I can find out the objects and quantity but the question is: I want a query which returns all the objects and should show quantity 0(zero or null) for those objects which don't falls under that date range. So I thought to use a decode function but it only uses the relational operation, I thought something like this but it's wrong,
select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
Does anyone have any idea how can I make this work in single sql statement?
Thanks for your help.select object,decode(created_date,BETWEEN '01/01/2008' TO '08/01/2008',quantity,0) from table;
Does anyone have any idea how can I make this work in single sql statement?If you're still interested in using DECODE over Case when, please try this.
Check for employees hired between 6/17/1987 and 9/30/1987
SQL> select first_name||' '||last_name as emp_name, hire_date,
2 decode(sign((to_date('06/17/1987','MM/DD/YYYY')-1)-hire_date)
3 + sign((to_date('09/30/1987','MM/DD/YYYY')+1)-hire_date)
4 ,0,'Between','Not between') as IsBetween
5 from employees
6 where hire_date <to_date('01/01/1991','MM/DD/YYYY')
7 /
EMP_NAME HIRE_DATE ISBETWEEN
Steven King 17-JUN-87 Between
Neena Kochhar 21-SEP-89 Not between
Alexander Hunold 03-JAN-90 Not between
Jennifer Whalen 17-SEP-87 BetweenMessage was edited by:
Bobbydj -
Odd Error with decode function in Order By Clause
I am trying to compile a procedure and can't get around an error with a dynamic order by that doesn't make much sense to me. I can repoduce the error with a plain select statment in sql plus so I can rule out a declaration error. Here is an example with 2 numeric columns and a date column.
select task_id, display_date, remark_id from task_list
where task_id > 1000
order by decode('Task_ID', 'Task_ID',Task_ID, 'Display_Date', Display_Date, 'Remark_ID',Remark_ID)
returns the error:
select task_id, display_date, remark_id from task_list
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
I'm not sure why this error is occuring, because it doesn't even hit the Display_Date field in the Decode statment. If I take the Display_date out of the statement, it runs properly. If I change Display_Date to To_Char(Display_Date) it also runs fine, but the sorting is incorrect. Also I'm running 9.2, and do not want to use dynamic sql to build my query. Any Ideas out there as to the cause of this error?I did find a workaround to this issue by breaking the decode statment into three separate statement, but I still think that the way it is written above should work, and may be a bug, or an error that I don't understand fully.
The Order by was rewritten like this:
order by decode(pSort, 'Task_ID',Task_ID), decode(pSort, 'Display_Date', Display_Date),
decode(pSort, 'Remark_ID',Remark_ID);
Thanks -
Hello!!
Can anyone please help me with the decode statement
Table 1 Table2 Table3
Id Id Code
Volume Code
For each of the codes in Table3 I need to find the corresponding Volume. Can I use decode in the select statement to this,
Eg Code Volume
ABC 20
XYZ 10 etc etc.
Thankyou all in advance.Your table structure is a little unclear from your post. I am assuming that what you posted was:
Table 1
Id
Table2
Id
Code
Table3
Volume
Code You can use decode if there are only a few values for table3. For example,
SELECT table2.code,
DECODE(table2.code,'ABC',10,'XYZ',20,'UNKNOWN') volume
FROM table2
or even
SELECT code,DECODE(code,null,'NONE',
(SELECT volume FROM table3 where code=table2.code)) volume
FROM table2However, the first will break, or at least be incorrect, if anyone adds a new code/volume pair, and is tedious if you have more than a few values. The second will likely be pretty slow if there are a large number of records in table3. The best solution would be to use a simple join rather than decode. Something like:
SELECT table1.id, table2.code, table3.volume
FROM table1, table2, table3
WHERE table1.id = table2.id and
table2.code = table3.code -
Doubt with Decode function(By Naren)
Hi all
i have the following query
SELECT S.SCHOOLNAME,
SUM(DISTINCT DECODE(LR.QUESTIONNAIRE_ID,62, L.WORK_SPACE))/5 As LITSUM
FROM TQDB_LEARNER_RESPONSE L LEFT JOIN TQDB_LEARNER_RESPONSERS LR ON LR.RESPONSER_ID=L.RESPONSER_ID LEFT JOIN
SCHOOLS S ON S.SCHOOLID=LR.SCHOOL_ID
GROUP BY S.SCHOOLNAME
It is working fine if i fixed QUESTIONNAIRE_ID AS 62.
Can i use parameter in place of 62 like :QID
When i execute this query and gave 62 as parameter value it should give LITSUM depend on 62,And if i wont give value(null or blank) it should give result irrespective of QUESTONNAIRE_ID.
I tried it by changing above query like following.
SELECT S.SCHOOLNAME,
SUM(DISTINCT DECODE(LR.QUESTIONNAIRE_ID,:QID, L.WORK_SPACE))/5 As LITSUM
FROM TQDB_LEARNER_RESPONSE L LEFT JOIN TQDB_LEARNER_RESPONSERS LR ON LR.RESPONSER_ID=L.RESPONSER_ID LEFT JOIN
SCHOOLS S ON S.SCHOOLID=LR.SCHOOL_ID
GROUP BY S.SCHOOLNAME
But it is not working properly,not giving expected result.
Pls help me.never noticed that bit, sorry.
I am assuming that your questionnaire_id is not null, so when you pass null there are no records with a questionnaire_id of null.
if you want to total all L.WORK_SPACE if you pass null then try this
SELECT S.SCHOOLNAME,
SUM(DISTINCT DECODE(:QID, NULL, L.WORK_SPACE, DECODE(LR.QUESTIONNAIRE_ID, :QID, L.WORK_SPACE))) / 5 AS
LITSUM
FROM TQDB_LEARNER_RESPONSE L LEFT JOIN TQDB_LEARNER_RESPONSERS LR ON LR.RESPONSER_ID = L.RESPONSER_ID LEFT JOIN SCHOOLS S ON S.SCHOOLID = LR.SCHOOL_ID
GROUP BY S.SCHOOLNAME; -
My table has columns A,B & C with data as below
A B C
X 1 1
1 X 1
1 1 X
Result should be all ‘Z’ inserted right to ‘X’ & must look like as below
A B C
X Z Z
1 X Z
1 1 X
Please check the below query & help me to modify the query as the result for column b is not correct.
select a,decode(b,'1','Z','X') b,decode(c,'1','Z','X') c
from test;try
With Data As
Select 'X' A,'1' B, '1' C From Dual Union All
Select '1','X','1' From Dual Union All
Select '1','1','X' From Dual
Select A, (Case When A ='X' And B='1' Then 'Z' Else B End) B,
(Case When (A='X' Or B='X') And C='1' Then 'Z' Else C end)C
from dataVivek L -
Problem with decode function while dispaly the data ( urgent )
Hi friends ,
I want the output like this.
sample:
CLIENT CODE: 00027
PLAN CODE: 01
SSN Last Name First Name TYPE Frequency Amount
123-45-6036 Perrault Julia D M 250.00
123-45-6036 Perrault Julia D Q 400.00
CLIENT CODE: 00027
PLAN CODE: 02
SSN Last Name First Name TYPE Frequency Amount
123-45-6036 Perrault Julia D S 1000.00
123-45-7042 Testaverde Alexander D B 50.00
this is my query:
SELECT distinct pln.plan_code,
ind.ssn,
ind.last_name,
ind.first_name,
pln.clt_client_id,
DECODE(rp.account_code,'AMNT',rp.userid,NULL) amount,
DECODE(rp.account_code,'FREQ',rp.userid,NULL) frequence,
DECODE(rp.account_code,'TYPE',rp.userid,NULL) type
FROM rp_extract_recon rp,
plan pln,
indicative ind
where ind.indicative_id in ( select distinct
rp1.ind_indicative_id
from rp_extract_recon rp1
where rp1.rp_extract_recon_id =
rp.rp_extract_recon_id )
and ind.clt_client_id = pln.clt_client_id
and pln.plan_id = rp.pln_plan_id
and rp.bat_batch_info_id = 14078
and rp.rp_report_type_code = 'TEST'
output for above query:
PLAN_C SSN LAST_NAME FIRST_NAME CLT_C AMOUNT
FREQUENCE TYPE
01 123456036 Perrault Julia 00027 250.00
01 123456036 Perrault Julia 00027 400.00
01 123456036 Perrault Julia 00027 M
01 123456036 Perrault Julia 00027 Q
01 123456036 Perrault Julia
00027 D
02 123456036 Perrault Julia 00027 1000.00
02 123456036 Perrault Julia 00027 S
02 123456036 Perrault Julia
00027 D
02 123457042 Testaverde Alexander 00027 50.00
02 123457042 Testaverde Alexander 00027 B
02 123457042 Testaverde Alexander
00027 D
11 rows selected.
11 rows selected.
how can i get the above ouput .
i want the type,frequency,amount values in one line.
thanks for u r kind help
sriniHi Srini,
Add Max in the begining and group by at the end of statement.
Please let me know in both cases if it works or not.
thanks
for example
SELECT distinct pln.plan_code,
ind.ssn,
ind.last_name,
ind.first_name,
pln.clt_client_id,
MAX(DECODE(rp.account_code,'AMNT',rp.userid,NULL) amount,
DECODE(rp.account_code,'FREQ',rp.userid,NULL) frequence,
DECODE(rp.account_code,'TYPE',rp.userid,NULL) type )
FROM rp_extract_recon rp,
plan pln,
indicative ind
where ind.indicative_id in ( select distinct
rp1.ind_indicative_id
from rp_extract_recon rp1
where rp1.rp_extract_recon_id =
rp.rp_extract_recon_id )
and ind.clt_client_id = pln.clt_client_id
and pln.plan_id = rp.pln_plan_id
and rp.bat_batch_info_id = 14078
and rp.rp_report_type_code = 'TEST'
GROUP BY
pln.plan_code,
ind.ssn,
ind.last_name,
ind.first_name,
pln.clt_client_id, -
Problem with decode function ( urgent )
Hi friends ,
this is my query
SELECT DECODE(rp.account_code,'TYPE',rp.userid,NULL) type,
DECODE(rp.account_code,'FREQ',rp.userid,NULL) frequence,
DECODE(rp.account_code,'AMNT',rp.userid,NULL) amount
FROM rp_extract_recon rp,
plan pln,
indicative ind
where ind.indicative_id in ( select distinct
rp1.ind_indicative_id
from rp_extract_recon rp1
where rp1.rp_extract_recon_id =
rp.rp_extract_recon_id )
and ind.clt_client_id = pln.clt_client_id
and pln.plan_id = rp.pln_plan_id
and rp.bat_batch_info_id = 14078
and rp.rp_report_type_code = 'TEST'
getting output like this:
TYPE FREQUENCE AMOUNT
S
D
1000.00
M
D
250.00
Q
D
400.00
B
D
50.00
12 rows selected.
i want the output like this :
TYPE Frequency Amount
D M 250.00
D Q 400.00
D S 1000.00
D B 50.00
how can get the above out put.
thanks for u r kind help
sriniTry formatting your columns
col type for a4
col frequence for a4
col amount for a4
although from looking at you query, I don't understand the
output you pasted. YOu have 3 decode statments based on the
same fields?
Hi friends ,
this is my query
SELECT DECODE(rp.account_code,'TYPE',rp.userid,NULL) type,
DECODE(rp.account_code,'FREQ',rp.userid,NULL) frequence,
DECODE(rp.account_code,'AMNT',rp.userid,NULL) amount
FROM rp_extract_recon rp,
plan pln,
indicative ind
where ind.indicative_id in ( select distinct
rp1.ind_indicative_id
from rp_extract_recon rp1
where rp1.rp_extract_recon_id =
rp.rp_extract_recon_id )
and ind.clt_client_id = pln.clt_client_id
and pln.plan_id = rp.pln_plan_id
and rp.bat_batch_info_id = 14078
and rp.rp_report_type_code = 'TEST'
getting output like this:
TYPE FREQUENCE AMOUNT
S
D
1000.00
M
D
250.00
Q
D
400.00
B
D
50.00
12 rows selected.
i want the output like this :
TYPE Frequency Amount
D M 250.00
D Q 400.00
D S 1000.00
D B 50.00
how can get the above out put.
thanks for u r kind help
srini -
Problem with decode function while dispaly the data
Hi friends ,
I want the output like this.
sample:
CLIENT CODE: 00027
PLAN CODE: 01
SSN Last Name First Name TYPE Frequency Amount
123-45-6036 Perrault Julia D M 250.00
123-45-6036 Perrault Julia D Q 400.00
CLIENT CODE: 00027
PLAN CODE: 02
SSN Last Name First Name TYPE Frequency Amount
123-45-6036 Perrault Julia D S 1000.00
123-45-7042 Testaverde Alexander D B 50.00
this is my query:
SELECT distinct pln.plan_code,
ind.ssn,
ind.last_name,
ind.first_name,
pln.clt_client_id,
DECODE(rp.account_code,'AMNT',rp.userid,NULL) amount,
DECODE(rp.account_code,'FREQ',rp.userid,NULL) frequence,
DECODE(rp.account_code,'TYPE',rp.userid,NULL) type
FROM rp_extract_recon rp,
plan pln,
indicative ind
where ind.indicative_id in ( select distinct
rp1.ind_indicative_id
from rp_extract_recon rp1
where rp1.rp_extract_recon_id =
rp.rp_extract_recon_id )
and ind.clt_client_id = pln.clt_client_id
and pln.plan_id = rp.pln_plan_id
and rp.bat_batch_info_id = 14078
and rp.rp_report_type_code = 'TEST'
output for above query:
PLAN_C SSN LAST_NAME FIRST_NAME CLT_C AMOUNT
FREQUENCE TYPE
01 123456036 Perrault Julia 00027 250.00
01 123456036 Perrault Julia 00027 400.00
01 123456036 Perrault Julia 00027 M
01 123456036 Perrault Julia 00027 Q
01 123456036 Perrault Julia
00027 D
02 123456036 Perrault Julia 00027 1000.00
02 123456036 Perrault Julia 00027 S
02 123456036 Perrault Julia
00027 D
02 123457042 Testaverde Alexander 00027 50.00
02 123457042 Testaverde Alexander 00027 B
02 123457042 Testaverde Alexander
00027 D
11 rows selected.
11 rows selected.
how can i get the above ouput .
i want the type,frequency,amount values in one line.
please help me.
thanks for u r kind help.
sriniHi.
I have not tested this my self, byt tryit.
SELECT pln.plan_code,
ind.ssn,
ind.last_name,
ind.first_name,
pln.clt_client_id,
MAX(DECODE(rp.account_code,'AMNT',rp.userid,NULL)) amount,
MAX(DECODE(rp.account_code,'FREQ',rp.userid,NULL)) frequence,
MAX(DECODE(rp.account_code,'TYPE',rp.userid,NULL)) type
FROM rp_extract_recon rp,
plan pln,
indicative ind
where ind.indicative_id in ( select distinct
rp1.ind_indicative_id
from rp_extract_recon rp1
where rp1.rp_extract_recon_id =
rp.rp_extract_recon_id )
and ind.clt_client_id = pln.clt_client_id
and pln.plan_id = rp.pln_plan_id
and rp.bat_batch_info_id = 14078
and rp.rp_report_type_code = 'TEST'
GROUP BY pln.plan_code,
ind.ssn,
ind.last_name,
ind.first_name
/Uffe
Maybe you are looking for
-
I need to restore back to 1.0 on my Nano as iTunes upgraded automatically to 1.2 and it's not compatible with my car device. Where can i download the ispw and ispw.signature files for 1.0 ?
-
Write to word document from oracle plsql
Hi, Please provide methods to create and write to a word document from ORACLE PLSQL.We tried using UTL FILE package but it writes as ascii text.
-
I'm trying the SP1 feature to pre-provision bitlocker during OSD, using an MDT integrated task sequence. It seems like the pre-provision part is working, but when the task sequence tries to enable bitlocker after installing the OS, it fails. ZTIBDE
-
Hi Everyone, I have scanned the boards for hours and have found that there are several keyboard issues, but they all seem to be unique. So this is my issue: My keyboard which came with my iMac (late 2009) has decided that it has a mind of its own. Th
-
Ios8.3 podcast play in car error
With the ios8.3 update, i can't play podcasts in my vehicle. Its downloaded on my phone and connected via cable but when trying to play it through the radio it doesn't show any podcasts listed.