DECODE Statement help
Hi All,
I have data like this 2 columns GRADE, ITEM_ID
A 001
B 002
C 003
A 006
Z 010
I want to query based on user criteria of what grade they needed. for some reason i need if users choose Z as criteria, i need to list all items with out grade A.
SELECT * FROM my_tbl WHERE grade = DECODE(:userprompt,'A','A','B','B'...'Z', ??);
?? - im missing how to put condition, i tried with '* A' but nothing is listing, obviously it is taking as GRADE = '* Z' and not returning any rows.
Oracle 10 G.
PLS NOTE * = Not equal symbol, for some reason this is not showing up when i post the thread.,
Thanks in ADvance.
Edited by: [email protected] on Sep 4, 2008 10:30 AM
Tony Andrews wrote:
An alternative without DECODE:
SELECT * FROM my_tbl
WHERE ((:userprompt = 'Z' and grade != 'A') or (grade = :userprompt));The above will not work if grade can be null (e.g. item is not graded):
SQL> exec :userprompt := 'Z'
PL/SQL procedure successfully completed.
SQL> with my_tbl as (
2 select 'A' grade, '001' item_id from dual union all
3 select 'B' grade, '002' item_id from dual union all
4 select 'C' grade, '003' item_id from dual union all
5 select 'A' grade, '006' item_id from dual union all
6 select 'Z' grade, '010' item_id from dual union all
7 select null grade, '011' item_id from dual
8 )
9 SELECT * FROM my_tbl
10 WHERE ((:userprompt = 'Z' and grade != 'A') or (grade = :userprompt));
G ITE
B 002
C 003
Z 010To account for null grade:
SQL> exec :userprompt := 'Z'
PL/SQL procedure successfully completed.
SQL> with my_tbl as (
2 select 'A' grade, '001' item_id from dual union all
3 select 'B' grade, '002' item_id from dual union all
4 select 'C' grade, '003' item_id from dual union all
5 select 'A' grade, '006' item_id from dual union all
6 select 'Z' grade, '010' item_id from dual union all
7 select null grade, '011' item_id from dual
8 )
9 SELECT * FROM my_tbl
10 WHERE ((:userprompt = 'Z' and (grade != 'A' or grade is null)) or (grade = :userprompt));
G ITE
B 002
C 003
Z 010
011SY.
Similar Messages
-
I had the table as follows :
ITEMCODE TEDCODE AMOUNT
AAAA BED 12345
AAAA EDU CESS 1234
AAAA SHECESS 123
AAAA CST 3% 12456
AND SO ON.
Now i want to convert the rows in Column heading TEDCODE to columns. But i want the specific rows like BED, EDU CESS, SHECESS in one column suppose EXCISE, and the other row i.e CST 3% in other column under heading TAX.
Please help me how i can do it using Decode statement.SQL> create table t (itemcode varchar(4),tedcode varchar(10),amount number);
Table created.
SQL> insert into t values('AAAA','BED',12345);
1 row created.
SQL> insert into t values('AAAA','EDU CESS',1234);
1 row created.
SQL> insert into t values('AAAA','SHECESS',123);
1 row created.
SQL> insert into t values('AAAA','CST 3%',12456);
1 row created.
SQL> select itemcode, tedcode,decode(tedcode,'CST 3%',null,amount) EXCISE, decod
e(tedcode,'CST 3%',amount,null) TAX from t;
ITEM TEDCODE EXCISE TAX
AAAA BED 12345
AAAA EDU CESS 1234
AAAA SHECESS 123
AAAA CST 3% 12456 -
Help! format of decode statements with between or signs ?
Looking for an example of a decode statement with either a between/and or < > signs in the comparison field.
I have tried several variations, but none seem to work. Any help would be appreciated.
Here's a sample that doesn't work.
Select course_no, section_no, capacity, decode(capacity, (capacity < 12), Less than 12, (capacity between 13 and 15), 13-15, More than 15) from section;
Thanks in advanceHi,
There is a limitation to the Case function, as it cannot be used in the PL/SQL (atleast upto Oracle 8i). Therefore, the only option left is Decode.
If you have to compare the values like capacity < 12 then display Less than 12. You can write it using combination of other functions.
For e.g. follow the steps below:-
1)If you deduct 12 from capacity : (capacity-12) [it will give result as +ve, 0 or -ve.]
2)If you have to narrow down the values to above three options, use function sign:
SIGN(capacity-12) [will result into +1, 0 or -1]
3)Then compare the values using Decode:
DECODE(SIGN(capacity-12),-1,Less than 12,Greater than or equal to 12)
The Decode can be used inside Decode(nested Decode) in above steps to compare more than one values (range between) to get the desired result. Hope have made it clear enough. If not, please let me know.
Cheers.
Yogesh D. -
Need help in decode statement to check condition
Hi All,
I have table emp with below structure
empno
work_phone_no
Home_phone
cell_phone
For the employee if work_phone & Home Phone is null then in place of work_phone we need to send the cell_phone number.
How to use the decode statement in this scenario to test for Home_phone
condition in the select statement.
select DECODE (work_phone_no,
null, cell_phone_no,office_phone_no )
from emp
ThanksHi,
Given the data you posted:
empno work_phone_no home_phone_no cell_phone_no
1 null 80032108556 8003210855
2 null null 8003210000 and your requirement is
"If work & Home nunbers are null then display cellhone number in place of workphone number "
, then, as John said, you do not want the results you posted. You want:
1 null 80032108556
2 8003210000 null That is, on the first row, where empno=1, the work_phone_no is NULL because the home_phone_no is NOT NULL, and cell_phone_no is only to be shown when both of the other phone numbers are missing.
To get the results above, use CASE:
SELECT empno
, CASE
WHEN work_phone_no IS NULL
AND home_phone_no IS NULL
THEN cell_phone_no
ELSE work_phone_no
END AS work_phone_no
, home_phone_no
FROM ...If you really do want the results you posted, then, as John said, use NVL. -
I have a scenario like
if account no is 0001 or 0002 or 0003 then account_flag <> 'Y'
ELSE IF account no is 00009 then account_status <> 'Y'
How to write DECODE statement for this in Where part of select statement
Note: account_flag and account_status are 2 different columns in same tableHi,
I assumed you mean to update the values?
You can use case for that:
MHO%xe> select * from same_table;
ACCOU A A
0001 N N
0002 N N
0003 N N
0004 N N
0005 N N
0006 N N
0007 N N
0008 N N
0009 N N
0010 N N
10 rijen zijn geselecteerd.
Verstreken: 00:00:01.90
MHO%xe> update same_table
2 set account_flag = case when account_no in ('0001', '0002', '0003')
3 then 'Y'
4 else account_flag
5 end
6 , account_status = case when account_no = '0009'
7 then 'Y'
8 else account_status
9 end;
10 rijen zijn bijgewerkt.
Verstreken: 00:00:00.15
MHO%xe> commit;
Commit is voltooid.
Verstreken: 00:00:00.09
MHO%xe> select * from same_table;
ACCOU A A
0001 Y N
0002 Y N
0003 Y N
0004 N N
0005 N N
0006 N N
0007 N N
0008 N N
0009 N Y
0010 N N
10 rijen zijn geselecteerd.
Verstreken: 00:00:00.07
MHO%xe> desc same_table
Naam Null? Type
ACCOUNT_NO VARCHAR2(5)
ACCOUNT_FLAG VARCHAR2(1)
ACCOUNT_STATUS VARCHAR2(1) -
How to determine INT and FLOAT in a DECODE statement
Can anyone help me with the following problem:
I have a column defined as a NUMBER(15,4). I am trying to determine the java type using the getScale() method in ResultSetMetaData. When the scale is greater than 0, then it is an float, else it is an int. In this case, the getScale() method should return 4. However, when querying with a DECODE statement on the NUMBER(15,4) field, the getScale() method always returns 0 and the value is therefore converted to an int. Anyone knows how to solve this problem?
ThanksAlcides,
Oracle NUMBER data type is mapped to "java.math.BigDecimal" class.
You will find more information in the JDBC Developer's Guide and Reference which is part of the Oracle documentation and available from:
http://www.oracle.com/technology/documentation/index.html
Good Luck,
Avi. -
please can someone kindly code the following line of SQL using the DECODE statement:
case when Student_Status in ('Registered','Repeat','Referral', 'Deferral')and GRADE <> 'MISSING' then EXAM1_RESULT else Null end as Valid_EX1
It will be most appreciated.
Thanksuser607929 wrote:
I'm ever so sorry, I keep sending the same Wrong thing.
This is what i need translated to DECODE:
"case when
Student_Status in ('Registered','Repeat','Referral', 'Deferral') and GRADE is not 'MISSING'
then EXAM1_RESULT
else Null end as Valid_EX1"
the issue is with the NOT 'MISSING' string.Check this. Just re-arrange of Andrés Muchiut post
DECODE (
STUDENT_STATUS
,'Registered',DECODE(GRADE,'MISSING',null,EXAM1_RESULT)
,'Repeat' ,DECODE(GRADE,'MISSING',null,EXAM1_RESULT)
,'Referral' ,DECODE(GRADE,'MISSING',null,EXAM1_RESULT)
,'Deferral' ,DECODE(GRADE,'MISSING',null,EXAM1_RESULT)
, null) Valid_EX1Hope this may help you... -
Decode statement in a mapping involve Source Text File & Table.
Hi All,
Oracle 9i Warehouse Builder Client: 9.2.0.4.0
Oracle 9i Warehouse Builder Repository: 9.2.0.2.0
Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
Does OWB allow to use Decode statement in a mapping involves Source Text File and Oracle Table.
My understanding is it's not possible, since OWB makes use of Sqlloader. For this work arround could be make use of External Table instead of Text Flat File.
However I came across an old posting (June-2003) which says that this feature is available OWB 9.2.
Following is the url:
IF THEN LOGIC from Flat File to Table
Can someone please confirm this?
Thanks in Advance.
Regards,
VidyanandHi all,
If you want to validate correctly this mapping you must to :
1. Right click on the mapping, then Configure
2. Right click on Sql Loader Data Files, then Create
3. Verify that the location name is correct and complete the Data File Name
4. OK
The validation is now OK.
I hope it will be help you
Best Regards
Samy -
Problem with DECODE statement while migrating forms to 6i
Hi,
I'm migrating a form from 5 to 6i. When I compiled the form, I got this error witha decode statement.
The error is
Error 307 at line 15 column 7
too many declarations of "DECODE" match this call
The trigger has this code:
IF :PRUN_RECS_INSERTED = 'Y' THEN
RETURN ;
END IF ;
INSERT INTO GJBPRUN
( GJBPRUN_JOB,
GJBPRUN_ONE_UP_NO,
GJBPRUN_NUMBER,
GJBPRUN_ACTIVITY_DATE,
GJBPRUN_VALUE )
SELECT :KEYBLCK_JOB,
:ONE_UP_NO,
GJBPDFT_NUMBER,
SYSDATE,
DECODE(GJBPDFT_VALUE, 'SYSDATE',
DECODE(GJBPDEF_LENGTH,'11',TO_CHAR(SYSDATE,'DD-MON-YYYY'), SYSDATE),
GJBPDFT_VALUE)
FROM GJBPDFT G, GJBPDEFEdited by: Charan on Mar 16, 2011 9:15 AMHi Charan
i think it's all about using both CHARACTER and DATE values at the same time in a DECODE statment u should either use char or date datatype.
DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle Database returns the corresponding result. If no match is found, then Oracle returns default. If default is omitted, then Oracle returns null.
e.g.
If expr and search are character data, then Oracle compares them using nonpadded comparison semantics.
expr, search, and result can be any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
The string returned is of VARCHAR2 datatype and is in the same character set as the first result parameter.
for more pls have a look here
Hope this helps,
Regards,
Abdetu... -
How to replace huge decode statements with lookups to some simple code/key
I have a legacy PL/SQL application, composed of many very huge decode statements. And the most terribe one is that the guys who develops the application left the company now, leaves no documentation.
We are trying to read and understand those PL/SQL programs, and I'm asked to replace those huge decode statements with lookups to some simple code/key tables? But I have no idea about how to design such code/key tables. Is there any one who has similar experience may help me? Besides code/key tables, any idea will be welcome.
Thank you very much!Not sure what your data looks like but sometimes decode can be replaced with more appropriate functions, ie;
SQL> with t as (
select 'DAY' a, 30 b, null c, null d from dual union all
select null a, null b, 'MONTH' c, 12 from dual)
select coalesce(b,d)
from t
COALESCE(B,D)
30
12
2 rows selected.
SQL> with t as (
select 'DAY' a, 30 b, null c, 0 d from dual union all
select null a, 0 b, 'MONTH' c, 12 from dual)
select greatest(b,d)
from t
GREATEST(B,D)
30
12
2 rows selected. -
Decode statement in Select line of a View Object Query
I attempted to create a view object in expert mode with a customized query.
The query had a decode statement in the select line of the query. The view
object compiled correctly but gave an error when run.
ex: select .... decode(CrpSchools.SCHOOLS_ID,null,CrpCustSchools.SCHOOL_NAME,CrpSchools.SCHOOL_NAME) SCHOOL_VALUE, ...
from ....
where ....
The error was that school_value does not exist in the statement. I got the error when
doing a vo.executeQuery().
When I removed the decode statement everything worked correctly. Does anyone know if
the decode statement cannont be used in the select line of a query in a view object?
Or maybe I was linking the query column (SCHOOLS_ID) up to the view attribute (SCHOOL_VALUE) incorrectly in the
Attribute Mappings tab of the VO wizard?There should be no problem using a DECODE() statement, provided that you've aliases the column as you have done.
At design time, if you click on the (Test) button, does your query test ok?
Are you by chance applying a custom where clause at runtime?
If so, are you saying:
setWhereClause("yourtable.column_alias = ?";
or are you doing:
setWhereClause("column_alias = ?";
for an expert-mode query, you'll need to use the latter syntax. -
Logical Standby SQL Apply Using Incorrect Decode Statement
We are seeing statements erroring out on our logical standby that have been rewritten (presumably by sql apply) with decode statements that don't appear to be correct. For example, here is one of the rewritten statements.
update /*+ streams restrict_all_ref_cons */ "CADPROD"."OMS_SQL_STATEMENT" p
set *"APPLICATION"=decode(:1,'N',"APPLICATION",:2)*,
"STATEMENT"=dbms_reputil2.get_final_lob(:3,"STATEMENT",:4)
where (:5='N' or(1=1 and (:6='N' or(dbms_lob.compare(:7,"STATEMENT")=0)or(:7 is null and "STATEMENT" is null)))) and(:8="APPLICATION")
The problem comes in, we believe, with the attempt to write the value "APPLICATION" to the application column which is only a 10 character field. the value for the :1 bind variable is "N" and the value for :2 is null.
We see the following error on the logical standby:
ORA-00600: internal error code, arguments: [kgh_heap_sizes:ds], [0x01FCDBE60], [], [], [], [], [], []
ORA-07445: exception encountered: core dump [ACCESS_VIOLATION] [kxtoedu+54] [PC:0x2542308] [ADDR:0xFFFFFFFFFFFFFFFF] [UNABLE_TO_READ] []
ORA-12899: value too large for column "CADPROD"."OMS_SQL_STATEMENT"."APPLICATION" (actual: 19576, maximum: 10)
Is this a configuration issue or is it normal for SQL Apply to convert statements from logminer into decode statements?
We have an Oracle 10.2.0.4 database running on windows 2003 R2 64-bit os. We have 3 physical and 2 logical standby's, no problems on the physical standbys.Hello;
I noticed some of your parameters seem to be wrong.
fal_client - This is Obsolete in 11.2
You have db_name='test' on the Standby, it should be 'asadmin'
fal_server=test is set like this on the standby, it should be 'asadmin'
I might consider changing VALID_FOR to this :
VALID_FOR=(ONLINE_LOGFILES,ALL_ROLES)Would review 4.2 Step-by-Step Instructions for Creating a Logical Standby Database of Oracle Document E10700-02
Document 278371.1 is showing its age in my humble opinion.
-----Wait on this until you fix your parameters----------------------
Try restarting the SQL Apply
ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATEI don't see the parameter MAX_SERVERS, try setting it to 8 times the number of cores.
Use these statements to trouble shoot :
SELECT NAME, VALUE, UNIT FROM V$DATAGUARD_STATS;
SELECT NAME, VALUE FROM V$LOGSTDBY_STATS WHERE NAME LIKE ;TRANSACTIONS%';
SELECT COUNT(1) AS IDLE_PREPARERS FROM V$LOGSTDBY_PROCESS WHERE
TYPE = 'PREPERER' AND STATUS_CODE = 16166;Best Regards
mseberg
Edited by: mseberg on Feb 14, 2012 7:37 AM -
Calling a stored proc in a decode statement
I am having a problem calling a store procedure in a SQL statement. I am using Oracle's thin driver.
I have been able to do the following:
select col1,
col2,
SOME_STORED_PROC(var1,var2,col3)
from some_table
where col3 = var3
However, when I try to call a stored procedure with-in a decode statement, the call fails. I have tested the call from a sql prompt and it works fine but it does not work when I execute the query in my Java program.
Here is an example of what I am trying to do:
select col1,
col2,
decode((select col1
from some_other_table
where col2 = var1), 'X',
SOME_STORED_PROC(var1,var2,col3),
SOME_OTHER_STORED_PROC(var2,var4,col5))
from some_table
where col3 = var3
Does anyone know if this type of call is not supported in Oracle's thin driver?
Thanks,
Cory
nullI played around with a [parallel PL/SQL launcher|http://www.williamrobertson.net/feed/2008/08/parallel-plsql-launcher-update.html] a while ago, but I wouldn't call it production-ready.
You could also [submit procedure calls in background|http://www.williamrobertson.net/feed/2005/12/job-control-object.html] using DBMS_ALERT to track completion status. -
Hi
I am trying to achieve the following in my report:
If an employee has a surname of . (dot) or a first name of . (dot), the report should not display a dot. An employee's name is made up of surname, first name and middle name which should all be concatenated together. To try to achieve this, I have the following statement in my report:
decode(e.Surname, '.', ( LTRIM(RTRIM((INITCAP(e.FIRST_NAME)))||' '||INITCAP(e.MIDDLE_NAME)) ) ,
e.FIRST_NAME, '.', ( LTRIM(RTRIM((INITCAP(e.Surname)))||' '||INITCAP(e.MIDDLE_NAME)) ) ,
( LTRIM(RTRIM((INITCAP(e.SURNAME )))||', '||INITCAP(e.FIRST_NAME)||' '||INITCAP(e.MIDDLE_NAME)) ) ) as emp_name
FROM Employee e
Problem: The above statement is only working for those employees with surname of . (dot). It's not working for first names of dot. How can I use the decode statement OR is there any other way of doing it without using the CASE statement?
It seems my decode statement doesn't work with 2 different fields (surname, firstname) being tested within one decode statement.Thanks.Thank you so much InoL . I have used the ltrim with Replace but now have a new problem.
Because I am joining Surname, First name and middle name together and put a comma after the Surname, the name now appears as follows:
, Maria Ane (if Surname is a dot)
Boiler, (if first name is a dot)
I would like to get rid of a comma and only have comma when surname or first name does not have a dot, i.e. for those people with full names e.g. Blake, Anna Marie.
InoL, how can I achieve this? Thanks. -
Condition to check in decode statement
Hi All,
I have a requirement to check below condtion in decode , please help me on this
I need to check name field from a table if it is not equals to null and not equals to ABC% then i need to print some static value as 'XYZ'
ThnaksUse CASE
case when column_name not like 'ABC%' and column_name is not null then 'XYZ' else null end
or
decode(nvl(substr(column_name,1,3),'ABC'),'ABC',null,'XYZ')Edited by: jeneesh on Sep 14, 2012 5:03 PM
Tested
Maybe you are looking for
-
Ipod wont show up in itunes after installing update
okay so i hadn't updated my ipod in a couple of months (bad i know but we have really slow internet here and it takes forever to download things) then i downloaded the update a couple of days ago and it started installing then came up with an error s
-
My ipod touch shows 10.1 gb of memory, but when I try to transfer a movie from my computer to ipod touch I get a error message that says "Cannot be synced because there is not enough free space to hold all of the items in the itunes library, addition
-
Update Windows 7 Pro to Windows 8 Pro
We are a small learning charity supporting 3 other charities who have been fortunate to achieve funding to purchase laptops. Having purchased 12 we seem unable to speak to anyone at the supplier or HP that can resolve a very simple problem! The lap
-
What are the limitations of using labview 8.5.1 developers suite verses a real-time module in field point applications? Can an exe. be loaded onto a field point controller or does the controlling program have to reside on a PC for example?
-
100 percent charge but shows 7 hours of time remaining??
Hi, My 10 day old macbook air shows 100 percent chraged but 7 hrs of battery remaining ; then after few mins 8 hrs goes on increasing initially till 12 or may be more then again starts decreasing. Is that normal behavior?????