PL/SQL :: Using Function On Decode Or Case In Query
Mar 3, 2013
I am using oracle 11G database,I have to check length of name column value from employee table and if length(name) > 39 then value should be substr(name,0,39) else value should be name only. i tried below code
select CASE when length(name) > 39,substr(name,0,39)
else name
END
from employee but its not working ..can I do this using decode too ? ,,which one would be better or this is not a right way ?
View 3 Replies
ADVERTISEMENT
Feb 13, 2011
How to use decode and case in "where" and "from" clause of a select statement.
I know the decode can't be used in where clause. In that case how we can use decode and case in from clasue that is: table definition
View 7 Replies
View Related
May 7, 2010
I am facing a problem while retrieving data from table using DECODE/CASE.
Table: PARAM_MSTR
MIN_VALMAX_VALPARAM_CODE DESCRIPTION DATATYPE
AB1000 HARD PARAMETERTEXT
CN1000 SOFT PARAMETERTEXT
0501001 CRYSTAL PARAMETERNUMBER
512001001 STONE PARAMETERNUMBER
Now I want to get the parameter description based upon the PARAM_CODE and a value passed which should be in range of MIN_VAL and MAX_VAL. Means when I pass PARAM_CODE=1000 and :parameter=A, then it should check the DATATYPE of the PARAM_CODE, in our case it is 'TEXT' so it should check the passed value between MIN_VAL and MAX_VAL like
:parameter BETWEEN MIN_VAL AND MAX_VAL and should return 'HARD PARAMETER'.
If I pass PARAM_CODE=1001, then the DATA_TYPE is 'NUMBER', so it will check the :parameter value as Number.
Like :parameter BETWEEN to_number(MIN_VAL) AND to_number(MAX_VAL)
For example:
PARAM_CODE :parametr Result
1000 A HARD PARAMETER
1000 C SOFT PARAMETER
1000 P NULL
1001 25 CRYSTAL PARAMETER
1001 99 STONE PARAMETER
1001 201 NULL
I have written a query using DECODE and CASE statement but it is not working properly.
SELECT * FROM param_mstr WHERE PARAM_CODE=1000 AND :parameter BETWEEN DECODE(DATATYPE,'NUMBER',CAST(MIN_VAL as NUMBER),MIN_VAL)AND DECODE(DATATYPE,'NUMBER',CAST(MAX_VAL as NUMBER),MAX_VAL)
View 3 Replies
View Related
Aug 19, 2010
I am having some records in the table. If the record num is
1--It should show the month as "Apr"
2--"May"
3--"Jun"
4--"July"
5--"Aug"
6--"Sept"
if it is having other than these 6 should show "0" for the remaining months.
View 10 Replies
View Related
Oct 20, 2011
I had written follwoing sql with CASE statments, but in oracle 8i i'm unable to use CASE in pl/SQL.
how to convert following sql to DECODE ? Or is there any option other than DECODE ?
SELECT ol.order_no,
ol.transaction_line_id,
ol.pd_actl_tonnage,
ol.real_tonnage_high,
ol.req_tonnage,
il.id,
il.transaction_ref,
[code].....
View 18 Replies
View Related
Dec 14, 2012
Here is one an example with CASE WHEN statement.
SELECT empno,ename,sal,
CASE
WHEN sal BETWEEN 800 AND 1200
THEN 'Lowest Pay'
[Code]...
even DECODE can be a replacement for this scenario, but it cannot have as flexibility as CASE can have.
Now, I want Equivalent code for DECODE..
View 11 Replies
View Related
May 14, 2013
my query is some thing like this but having more column in select. when i am firing this query it is giving result but that is not proper,\.
my problem is , like if there are 3 more values for uh.sflowtype (0,1,2) then group by is not working for them and those are coming in different row , i need them to be combined
query is :
select substr(uh.sstartdatetime,1,8) DateTime,
( case
when uh.sflowtype=7 then 'sms'
when uh.sflowtype=9 then 'mms'
when uh.sflowtype=10 then 'gprs'
[code]....
result :
DATETIME FLOWTYPE
-------- --------
20130507 voice
20130507 voice
20130507 voice
20130507 sms
20130507 mms
but i need
20130507 voice
20130507 sms
20130507 mms
so what should i do?
View 8 Replies
View Related
Feb 21, 2013
case when age <= 17 then '<= 17'
when age >= 40 then '>= 40'
else to_char(t.age)
end age
the case statement above doesn't work in my 8.1.7 cursor statement within my pl/sql block so I need an equivalent decode
View 10 Replies
View Related
Apr 6, 2012
Data Type Consistency CASE and Decode
CASE expects data type consistency, DECODE not expecting.
Obviously both functions handling data types are different let it be
SQL> select decode(2,1,1, 2,'2', ' three' )"RESULT" from dual;
RESULT
---
2
SQL> select case 2
2 when 1 then 1
3 when 2 then 2
4 else 3
5 end "RESULT" from dual;
RESULT
2
SQL> select case 2
2 when 1 then 1
3 when '2' then 2
4 else 3
5 end "RESULT" from dual;
ERROR at line 3:
ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
know cause of error here????I mean " every time case exp checking data type consistency"
my thought is
I am trying to get same output but different methods. Yes, clearly states this is one of the Oracle bug!
I want to know how oracle handles here ???? i mean 3rd query.Purely i am testing this function with dual(dummy) table...
obviously, no possibilities for different data type.next one i am not sure about Oracle compares int variables to int variables, char variables to char variables,
I think so .... that's why oracle throws error. am i right ??ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
see this query
SQL> select case 2
2 when 1 then 1
3 when '2' then 2
4 else 3
5 end "RESULT" from dual;
// clearly i am stating what's error on 3rd line ?????Even if I refer reference books some concepts are blind to understand.
View 2 Replies
View Related
Apr 5, 2012
Data Type Consistency CASE and Decode...CASE expects data type consistency, DECODE not expecting.Obviously both functions handling data types are different..let it be
SQL> select decode(2,1,1, 2,'2', ' three' )"RESULT" from dual;
[color=red]RESULT
---
2
SQL> select case 2
2 when 1 then 1
3 when 2 then 2
4 else 3
5 end "RESULT" from dual;
[code]...
I am trying to get same output but different methods. Yes, clearly states this is one of the Oracle bug! Oracle compares int variables to int variables, char variables to char variables, If any discrepancy between the two data types, then the query will fail but here not different data types.
Even if I refer reference books some concepts are blind to understand.
View 5 Replies
View Related
Oct 31, 2011
How can i use the decode function?
for example
I have the value of 1000 then the numbers 50-100 will be 'A' and 1-49 = 'B'?
View 9 Replies
View Related
Apr 4, 2013
DECODE(:P_PERIOD_TYPE,'PJTD','PROJECT-TO-DATE','PTD','PERIOD-TO-DATE','YTD','YEAR-TO-DATE')
what does it mean..
View 3 Replies
View Related
Aug 19, 2010
select
DECODE(PAA.C_TYPE_DESC,'TXIS CASUAL LEAVE',NVL(PAA.ABSENCE_DAYS,0)) TCL,
DECODE(PAA.C_TYPE_DESC,'TXIS SICK LEAVE',NVL(PAA.ABSENCE_DAYS,0)) TSL,
DECODE(PAA.C_TYPE_DESC,'TXIS PRIVELEGE LEAVE',NVL(PAA.ABSENCE_DAYS,0)) TPL
from PER_ABSENCE_ATTENDANCES_V PAA
i want to use nvl function within decode function
means if C_TYPE_DESC is 'TXIS CAUSAL LEAVE' it return ABSENCE_DAYS but if ABSENCE_DAYS are null it will be return 0 else it should be return Absence_days
View 12 Replies
View Related
Nov 8, 2009
What I have to do is this:
List supplier�s name, id, number of pending orders, number of completed orders and number of all orders from this supplier. (my query doesn't include the last part yet).
My tables are as follows:
Suppliers:
S_ID
NAME
ADDRESS
PROVINCE
PHONE_NUMBER
Orders:
ORD_ID
SUPPLIER_ID
ISSUING_EMP_ID
ORDER_DATE
ORDER_STATUS
and this is what I have so far:
select name, s_id
sum(decode(order_status, 'P',1,0)) pending,
sum(decode(order_status, 'C',1,0)) completed
from suppliers
where s_id in
(select supplier_id from orders);
but I still get an error that says "sum(decode(order_status, 'P',1,0)) pending,
*
ERROR at line 2:
ORA-00923: FROM keyword not found where expected"
I assume this is because I'm not properly referencing the orders table.
View 1 Replies
View Related
Aug 28, 2012
I have a table like Create temptbl (Client_status CHAR(1), Rollbak NUMERIC(3), count_org NUMERIC(3), count_member NUMERIC(3))
With Values
Cliet_Status Rollbak count_org count_member
------ ------- --------- ------------
Z 3 7 5
P 39 5 8
R 49 1 6
S 5 6 4
I need to use DECODE function for getting the result like
IF client_status NOT IN('Z', 'P')
THEN rollbak in(7, 39,49,10)and count_org is not null and count_member is not null.
ELSE NULL
View 3 Replies
View Related
Dec 13, 2011
I have 3 tables.
Table 1 have 3 columns
ID, CUS_NAME, LOC
insert into table1 values (001,ABC,North);
insert into table1 values (002,DEF,South);
insert into table1 values (003,GHI,West);
Table 2 have 3 columns
ID, CUS_NAME, LOC
insert into table2 values (001,ABC,North);
insert into table1 values (002,DEF,East);
insert into table1 values (003,JKL,South);
Table 3 is Result_Tab table having 8 columns
ID, TAB1_CUS_NAME, TAB2_CUS_NAME, Cus_Name_Res, TAB1_CUS_LOC, TAB2_CUS_LOC, Cus_LOC_Res, Comment.
I have written two cursors which fetches data from both the tables and compares each data between each other and inserts the value into the result table.
the code is as follow:
Insert into Result_Tab values
(T1.ID, T1.Cus_Name, T2.Cus_Name, decode(T1.Cus_Name,T2.Cus_Name,'Y','N'),T1.LOC, T2.LOC, decode(T1.LOC,T2.LOC,'Y','N'),Null);
Now I want the resul as follows:
ID T1.N T2.N N_Res T1.L T2.L L_Res Comment
001 ABC ABC Y North North Y Null
002 DEF DEF Y South East N Loc
003 GHI JKL N West South N Name, Loc
Is there a way wherein i could capture the column names in decode function when it doesn't match, so that I can insert the same in the comment column.
View 4 Replies
View Related
Oct 18, 2010
I am trying to use decode funtion with GROUP BY ROLLUP.
MY query is working fine when i use this two queris individually
SELECT SUM(SAL),DEPTNO,JOB FROM EMP GROUP BY ROLLUP ((DEPTNO),(DEPTNO,JOB));
SELECT SUM(SAL),DEPTNO,JOB FROM EMP GROUP BY ROLLUP((JOB),(DEPTNO,JOB));
But when i use Decode funtion so that i can combine above two scenarios it is not working
SELECT SUM(SAL),DEPTNO,JOB FROM EMP GROUP BY ROLLUP ( DECODE(:A,'S',((DEPTNO),(DEPTNO,JOB) ),((JOB),(DEPTNO,JOB) ) ) )
View 3 Replies
View Related
Oct 19, 2011
Can we call a function within decode statement. I am able to do the same for simple example function . But In my actual procedure it's giving the error message . Are there any restrictions to call function with in decode statement?
View 4 Replies
View Related
May 11, 2010
I am trying to use decode function in sql and inside decode function can I use select statement ?
here is my sql
select we.wf_entity_id, decode(object_type_id,
1, select audit_number from ea_audit_general where sys_audit_id=object_id
2,'test',
object_type_id
) from wf_entity we
where
[code]....
see this
decode(object_type_id,
1, select audit_number from ea_audit_general where sys_audit_id=object_id
2,'test',
object_type_id
)
will this work? Its not working for me?
View 2 Replies
View Related
Sep 16, 2010
I have Two cursor record block..which is attached in form..
My TASK IS
In my first Block, When DBCR Column = 'D' Then in backend this column value should be save as a '1'
WHEN DBCR Column = 'C' Then Then in backend this column value should be save as a '2'
My Both Field is on Data Block...
In Property palette of this field can we write any decode condition..so it reflects directly on database.
View 2 Replies
View Related
May 20, 2013
first_tbl
First_dt second_dt Item
01-JAN-09 01-JUL-09 item1
01-APR-09 01-DEC-09 item1
01-JUL-09 31-DEC-09 item1
15-DEC-09 10-MAY-10 item1
20-MAR-12 10-AUG-12 item2
31-JUL-12 15-DEC-12 item2
01-APR-09 31-DEC-09 item2
second_tbl
Start_dt end_dt item flag
01-AUG-09 01-NOV-09 item1 null
02-NOV-09 01-DEC-09 item1 null
01-DEC-09 15-DEC-09 item1 null
15-DEC-09 31-DEC-09 item1 null
01-JAN-10 10-MAY-10 item2 null
10-MAY-10 31-DEC-10 item2 null
01-JAN-12 15-MAR-12 item2 null
20-MAR-12 31-JUL-12 item2 null
I need o/p in such a way that,if first_dt or second_dt in between start_dt and end_dt, i need to make the flag as y(item is the common join) other wise 'N'.
I was able to make the flag as'Y' but not 'N'
Select A.first_Dt, A.second_Dt, B.Start_Dt, B.End_Dt, A.item, B.item,
Case When B.falg is null
Then 'N'
Else 'Y'
End
From firsttbl A,second_tbl B
Where
A.item=B.item
and
((A.first_Dt>=B.Start_Dt And A.first_Dt<=B.End_Dt)
Or
(A.second_Dt>=B.Start_Dt And A.second_Dt<= B.End_Dt));
View 13 Replies
View Related
Apr 3, 2012
I want to perform some operation with case statement. But I am confusing with ora 00932 error. My question is what data type should I use while performing case function?
SQL> select * from samp;
NAME EMPID SALARY DEPT
---
sony 10680 8200 sap
bala 10708 4300 .net
sam 10600 9000 oracle
chris 10655 5500 java
rose 10487 8700 oracle
[code]....
My big question is
different datatypes, then use consistent datatypes. For example, convert the character field to a numeric field with the TO_NUMBER function before adding it to the date field. Functions may not be used with long fields.
// just I am trying to perform basic operation. why oracle didn't support?
View 1 Replies
View Related
Jun 20, 2013
I am using: Desktop / Discoverer 4.1 / Windows XP.
I am attempting to add a new calculated column and have had some success with the CASE function but need to add additional criteria.
What I have that works is:
SUM(CASE WHEN Expenditure Type = 'Supplier Rebates' THEN Total Spend Plus Commit ELSE 0 END)
What I need to add are a few additional criteria. I attempted and failed with a few variants of this:
SUM(CASE WHEN Expenditure Type = 'Supplier Rebates' AND Capitalizable = 'Y' AND
Task Owing Company = '534' OR '915' THEN Total Spend Plus Commit ELSE 0 END)
The three criteria points that I am looking to includea are:
•Expenditure Type = 'Supplier Rebates'
•Capitalizable = 'Y'
•Task Owing Company = '534' OR '915'
View 8 Replies
View Related
Dec 26, 2012
How to get the required output with single query to acheive IF SAL>2500 and job='manager' then display job as 'seniormanager'.
CREATE TABLE EMP(EMPNO NUMBER(4) NOT NULL,ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7, 2),COMM NUMBER(7, 2),DEPTNO NUMBER(2));
INSERT INTO EMP VALUES(7521, 'WARD', 'SALESMAN', 7698,TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
INSERT INTO EMP VALUES(7566, 'JONES', 'MANAGER', 7839,TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
INSERT INTO EMP VALUES(7698, 'BLAKE', 'MANAGER', 7839,TO_DATE('1-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
INSERT INTO EMP VALUES(7782, 'CLARK', 'MANAGER', 7839,TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
INSERT INTO EMP VALUES(7788, 'SCOTT', 'ANALYST', 7566,TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
[code]....
View 2 Replies
View Related
Aug 12, 2010
I have used a decode statement in my query instead of using select * from <table_name> where value in <>
Instead I have used decode(value,<value1>, <value2>)
Which would be a better performance code?
View 5 Replies
View Related
Sep 2, 2011
small piece of PL SQL code. how to make this query.Requirement is that a concurrent program is run with parameters and one of them i_num_org_id is non mandatory so it can come as NULL...Now in an existing code which i have to change, it uses a query as
SELECT xyz
FROM abc_table
WHERE <various conditions>
AND DECODE(i_num_org_id,NULL,1,table.organization_id) = NVL(i_num_org_id,1);
Now with the above way, if the program is run with some value for i_num_org_id or run as normal query (with NULL as the value) inside a PLSQL procedure/package then it runs fine.This query if you run in Toad etc then too it will work fine but if it is made a dynamic SQL and then used as either EXECUTE IMMEDIATE or opened as a cursor then we get a "Missing expression". I created this small anonymous block to test this and this will go into missing expression error
declare
l_num_org_id NUMBER := NULL;
l_temp VARCHAR2(100);
l_sql varchar2(1000);
begin
l_sql := 'SELECT '||''''||'abcd'||''''||'
[code].....
how i can reformat this query so that even if NULL value comes for i_num_org_id then it is handled.I am aware about CASE but that cannot be used in WHERE clause i guess.
View 10 Replies
View Related
Jan 24, 2013
how can i define the following condition in query definitions tab to define query condition for the value which is linked from previous tab/report ,:P2_SR_ BUSINESS_UNIT is the Item which are getting value from another report Value.
BUSINESS_UNIT=DECODE(:P2_SR_BUSINESS_UNIT,'BS&I','Business Sales And Information','WS','Whole Sale')
I am new in apex and havent had any chance to create a simple report.Version Application Express 4.1.1.00.23
View 6 Replies
View Related
Mar 23, 2009
I have a query that will either return one record or zero records. When it returns zero records I want to replace my attributes with a sentinel, like 'N/A'. I tried the CASE statement but couldn't get anything to work
Sample (does not work):
select
(case when exists (select product from tbl_product where productid = '123') then product else 'N/A' end) product
from tbl_product
where productid= '123';
If one record exists it should produce: 'My Widget' (or whatever)
If zero records exist it should produce: 'N/A'
View 3 Replies
View Related
May 14, 2012
The query has a case statement in the where clause so that results can be filtered. If I pass "ut" for sso_id then the query returns 21 rows. If I remove the case statement and hard code "a.sso_id like lower('ut'||'%')" then the query returns 41 rows. The query should be returning 41 rows all the time.
Problem:
When passing "ut" as an SSOID parameter to the Procedure the query returns 21 rows.Taking the query and hard coding "a.sso_id like lower('ut'||'%')" the query returns 41 rows.
Result:
query should be returning 41 rows when "ut" is passed an an SSOID parameter.
Returns 21 rows
procedure SSO (SSOID in varchar2 default null,
Name in varchar2 default null,
Campus in varchar2 default null,
Department in varchar2 default null,
[code]...
Returns 41 rows
open Results for
select a.sso_id,
(a.name_last||', '||a.name_first) as name,
b.site,
[code]...
Test Data
CREATE TABLE ID
(
SSO_ID VARCHAR2(60 BYTE),
NAME_FIRST VARCHAR2(100 BYTE),
NAME_LAST VARCHAR2(100 BYTE),
[code]...
Test Data
CREATE TABLE NT
(
LOWER_NT_ID VARCHAR2(60 BYTE),
DEPARTMENT VARCHAR2(100 BYTE),
[code]....
View 3 Replies
View Related
Jan 4, 2013
I am trying to use the below query
update t_emp set TTL_FLG =
CASE
WHEN EXISTS
(SELECT 1 from Schema1.T_STG_LW_EMP E
WHERE E.Employee = Schema2.T_emp.EMPLOYEE_NUMBER
AND E.JB_CODE like '%TP%' or E.JB_CODE like '%DGD%' or E.JB_CODE like '%PDD%'
or E.JB_CODE like '%YND%'
)
THEN 'Y'
ELSE 'N'
END;
View 1 Replies
View Related