Can i use Decode in Where clause
Hi,
Can i use Decode in Where clause Please Do the need full on the same.
Thanks,
Sanjeev.
set serveroutput on
DECLARE
posn PLS_INTEGER := 0;
empid PLS_INTEGER := 178;
x NUMBER;
BEGIN
SELECT NVL(SUM(ah.quantity * ah.saleprice * ap.payoutpct), 0)
INTO x
FROM accessoryhistory ah, payoutpercentage ap,
sku s, store st
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)))
AND ah.statustype IN ('ACT', 'DEA')
AND ah.store = st.store
AND s.dbid = ah.dbid
AND s.sku = ah.sku
AND ap.productgroup = s.productgroup
AND ap.position = posn;
dbms_output.put_line(x);
END;
/http://psoug.org/reference/decode_case.html
Similar Messages
-
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))) -
Can we use Case in Where Clause along with Exists
Hi Everybody,
Can we use Case in the where clause with exists? As i have a requirement in which i have to check whether value exists in 6 views, now depending on some value(gns_type )of select clause i have to attach a paticular exists else the performance dies.
Please go through the query any suggestion appreciated.
Thanks
SELECT count(*)
FROM
(SELECT eah.changed_date,
decode(eua.is_deleted, 'N', decode(eah.alert_type, NULL, 'GN', 'R', 'GAR', 'G', 'GAG', 'Y', 'GAY'), 'Y', decode(eah.alert_type, 'R', 'GDR', 'G', 'GDG', 'Y', 'GDY', NULL, 'GN'), NULL, 'GN') AS
alert_type,
decode(eac.pta_line, 'N', '') ptaline,
eac.exp_type_desc,
eac.supplier_name,
eac.transaction_id,
eah.gns_type,
eac.po_amount,
eac.po_end_date,
eah.notes,
eua.is_deleted,
eac.expenditure_type,
eua.gns_alert_summary_id,
eah.changed_date alert_date,
eua.user_alert_id,
eah.reference_number,
decode(eac.cms_pta_line,'N','',eac.cms_pta_line) cms_pta_line,
cms_po_amount,
cms_po_end_date,
mgns.is_decommitted,
eac.gns_alert_id,
eah.gns_type source_name
FROM xxdl.xxdl_sc_gns_alerts_summary eah,
xxdl.xxdl_sc_gns_detail_alerts eac,
xxdl.xxdl_sc_gns_user_alerts eua,
xxdl.xxdl_sc_manage_gns_master mgns
WHERE eah.gns_alert_summary_id = eac.gns_alert_summary_id
AND eah.gns_alert_summary_id = eua.gns_alert_summary_id
AND eah.transaction_id = eac.transaction_id
AND eah.transaction_id = mgns.transaction_id)
a
WHERE(EXISTS
(SELECT 1
FROM xxdl_sc_mng_gns_pta_req_hc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS( SELECT 1
FROM xxdl_sc_mng_gns_pta_inv_hc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS(SELECT 1
FROM xxdl_sc_mng_gns_pta_req_sc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS(SELECT 1
FROM xxdl_sc_mng_gns_pta_inv_sc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS( SELECT 1
FROM xxdl_sc_mng_gns_pta_po_sc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
OR
EXISTS (SELECT 1
FROM xxdl_sc_mng_gns_pta_po_hc_v x
WHERE x.transaction_id = a.transaction_id
AND x.source_name = a.source_name
AND x.project_id = 69309
AND x.task_id = 242528
AND x.award_id = 34694)
AND TRUNC(alert_date) >= TRUNC(add_months(sysdate, -1))
AND TRUNC(alert_date) <= TRUNC(sysdate)
AND is_deleted = 'N'
ORDER BY changed_date DESCyou can do
WHERE
CASE WHEN (something) THEN
CASE WHEN EXISTS (SELECT * from ...) THEN 1 ELSE 0 END
WHEN (something else) THEN
CASE WHEN EXISTS (SELECT * from ...) THEN 1 ELSE 0 END
END = 1Looking at your current query, it looks like all those exist statements could be a lot neater, maybe like:
WHERE (69309,242528,34694) IN
(SELECT project_id,task_id,award_id FROM
(Select project_id,task_id,award_id,transaction_id,source_name
FROM
xxdl_sc_mng_gns_pta_req_hc_v
UNION ALL
Select project_id,task_id,award_id
xxdl_sc_mng_gns_pta_inv_hc_v
...) x
where a.transaction_id = x.transaction_id
and a.source_name = x.source_name
)or put the tuple in the where clause at the bottom -
Using decode in where clause with user defined function
Hi,
I have a below query which is failing as the function in the decode taking all cust_account_id as input parameter instead of the one which satisfies the condition in the inner query.So please provide a solution how can i pass only the selected one.
SELECT hca.cust_account_id
FROM hz_cust_accounts hca
WHERE hca.org_id=FND_PROFILE.value('ORG_ID')
AND hca.cust_account_id = (SELECT DISTINCT hcasa.cust_account_id
FROM hz_cust_acct_sites_all hcasa
WHERE hcasa.cust_account_id =hca.cust_account_id
AND hca.org_id = hcasa.org_id)
AND DECODE (hca.status , 'A', xx_ar_cust_audit_pkg.get_ship_to_order_type(hca.cust_account_id)) IS NOT NULL
Thanks,
AbhilashI'm having to guess without access to your tables, but I think changing the IN to a join should produce the same results. The JOIN should be evaluated before applying the WHERE clause, so this may resolve your problem.
SELECT hca.cust_account_id
FROM hz_cust_accounts hca
INNER JOIN hz_cust_acct_sites_all hcasa
ON hcasa.cust_account_id = hca.cust_account_id
AND hca.org_id = hcasa.org_id
WHERE hca.org_id = FND_PROFILE.value('ORG_ID')
AND DECODE (hca.status , 'A', xx_ar_cust_audit_pkg.get_ship_to_order_type(hca.cust_account_id)) IS NOT NULLAlternately, you could next part of the query and break the DECODE into the parent so it is evaluated after the inner query. This is likely uglier from a performance standpoint, though:
SELECT cust_account_id
FROM
SELECT hca.cust_account_id, hca.status
FROM hz_cust_accounts hca
WHERE hca.org_id=FND_PROFILE.value('ORG_ID')
AND hca.cust_account_id = (SELECT DISTINCT hcasa.cust_account_id
FROM hz_cust_acct_sites_all hcasa
WHERE hcasa.cust_account_id =hca.cust_account_id
AND hca.org_id = hcasa.org_id)
WHERE DECODE (status , 'A', xx_ar_cust_audit_pkg.get_ship_to_order_type(cust_account_id)) IS NOT NULL; -
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. -
Can we use concatenate in where clause of a select query
I have a select query as follows....
SELECT ebeln ebelp belnr vgabe bwart DMBTR
FROM ekbe
INTO TABLE i_ekbe
FOR ALL ENTRIES IN i_blck
WHERE ebeln = i_blck-ebeln AND ebelp =
i_blck-ebelp.
Now i want to retrieve data from BKPF ..can i write a select query something like....
select * from BKPF into itab
FOR ALL ENTRIES IN i_ekbe
where awkey = ( conatenate i_ekbe-belnr i_ekbe-gjahr )
OR ELSE...is there any other way to link table ekbe and bseg ???Hi Poonam,
SELECT ebeln ebelp belnr vgabe bwart DMBTR
FROM ekbe
INTO TABLE i_ekbe
FOR ALL ENTRIES IN i_blck
WHERE ebeln = i_blck-ebeln
AND ebelp = i_blck-ebelp.
select *
from BKPF
into table itab
FOR ALL ENTRIES IN i_ekbe
where belnr = i_ekbe-belnr.
If u want to check with AWKEY concatenate into seperate field and then use to check in where condition.
data: lw_awkey(20) type c.
CONCATENATE i_ekbe-belnr i_ekbe-gjahr INTO lw_awkey.
select *
from BKPF
into table itab
FOR ALL ENTRIES IN i_ekbe
where awkey = lw_awkey
Best regards,
raam -
Using a dummy where-clause to force the use of an index
I notice that an index only gets used when I use the index key in the where clause. Should I use a dummy where clause to force the index to be used?
SQL> create table emp (
2 empno NUMBER(5),
3 empname VARCHAR2(15) );
Table created.
SQL> create index idx_emp_no on emp (empno);
Index created.
SQL> insert into emp (empno, empname) values (1, 'Peter');
1 row created.
SQL> set autotrace traceonly;
SQL> select empno from emp;
Execution Plan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 13 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| EMP | 1 | 13 | 3 (0)| 00:00:01 |
SQL> select empno from emp where empno > 0;
Execution Plan
Plan hash value: 434430053
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_EMP_NO | 1 | 13 | 1 (0)| 00:00:01 |
As you can see, using a dummy where clause (empno > 0), I manage to reduce the cost from 3 to 1.Again. Be careful with comparisons. But I like the discussion. It is usefull to carefully look at what happens "behind the scenes" and find a sensible explaination for that.
Your new scenario now is different. By selecting one specific row instead of selection all you already made an assumption. That only one or few of the rows are returned. Andre's statement still is correct. Now the CBO will prefere the index scan.
The CBO now has to decide between two options.
Option a) use the index.
This means =>
Step 1: Access the index to find the value
Step 2: Access the table using the rowid that was found in the index leaf entry.
Option b) scan the full table
This means =>
Step 1: Iterate over all rows of the table
Step 2: Apply a filter condition (empno = :x)
We can see this execution plan by adding a hint to the query.
select /*+ FULL(emp) */ * from emp where empno = 1;This hint will force the CBO to access the table emp using a full table scan.
The cost in my environment for option B is 3.
Option A has a cost with 2. Therefore the CBO prefers Option A.
However this can easily change.
Consider the following
begin
for i in 1..1000 loop
insert into emp (empno, empname) values (1, 'Employee');
end loop;
end;
commit;
execute dbms_stats.gather_table_stats(user,'EMP');The run the select again.
SQL> set autotrace traceonly
SQL> select * from emp where empno = 1;
1001 Zeilen ausgewõhlt.
Ausf³hrungsplan
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1001 | 12012 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 1001 | 12012 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("EMPNO"=1)
Statistiken
1 recursive calls
0 db block gets
74 consistent gets
0 physical reads
0 redo size
18356 bytes sent via SQL*Net to client
1250 bytes received via SQL*Net from client
68 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1001 rows processedThe CBO now prefers the full table scan and not the index access.
If we force it to use an index then the cost increases.
SQL> set linesize 100
SQL> select /*+INDEX(emp) */ * from emp where empno = 1
1001 Zeilen ausgewõhlt.
Ausf³hrungsplan
Plan hash value: 2426388914
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1001 | 12012 | 5 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1001 | 12012 | 5 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_EMPNO | 1001 | | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("EMPNO"=1)
Statistiken
0 recursive calls
0 db block gets
139 consistent gets
0 physical reads
0 redo size
30550 bytes sent via SQL*Net to client
1250 bytes received via SQL*Net from client
67 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1001 rows processed
SQL>The cost using the index range scan is now 5. WHile the FTS is still 3. Therefore the CBO prefers to use the FTS. -
Can both in-row and off-row LOBs be used in a where clause?
We are preparing to upgrade ASE from version 15.5 to 16.0. One of the features that is very appealing has to do with the changes made concerning the handling of LOBs. In particular, the ability to use a LOB column in search arguments (where clause) is something our developers have longed for since the retirement of the full-text search option. So, in version 16.0, can any column with a datatype of text be used as a search argument? Are there any restrictions.
Thanks in advance.Hi Kevin,
The in-row LOB feature is mainly designed to save space when a text column has many short values, which is a particularly bad problem when the server uses a large page size (like 16K) as ASE would use a full 16K to store even a single bye "Y". I don't think the feature made any change in how text can be used in search arguments. i.e. you can still use LIKE and IS [NOT] NULL, but not others.
Example:
1> create table t (x text in row (8))
2> go
1> insert t values ("abc")
2> insert t values ("abcdefghijkl")
3> go
(2 rows affected)
1> select * from t where x like "%c%"
2> go
x
abc
abcdefghijkl
(2 rows affected)
1> select * from t where x !> "c"
2> go
Msg 257, Level 16, State 1:
Server 'redhead', Line 1:
Implicit conversion from datatype 'TEXT' to 'VARCHAR' is not allowed. Use the
CONVERT function to run this query.
Msg 306, Level 16, State 5:
Server 'redhead', Line 1:
TEXT, IMAGE and UNITEXT datatypes may not be used in a WHERE clause, except with
the LIKE expression and IS [NOT] NULL predicate on columns. -
A typical decode in where clause
Hi to all,
Could you anybody please give response to my query
select ast.account_num,sup.billing_status_text
from ast,sup
where
(sup.account_status=ast.account_status
or sup.account_status='99999')
Here sup.account_status,ast.account_status are notnull columns..
In the above query duplicates are coming as sup table has 2 rows , one row with matched value for ast.account_status and other row is with sup.account_status= '99999'.
I need to get only one row like
if matched row corresponding to ast.account_status is there in sup , then i need to get ,sup.billing_status_text corresponding to that matched ast.account_status
otherwise i shd get sup.billing_status_text corresponding to sup.account_status='99999' value in sup table
Can we use decode here to remove duplicates....
How to solve thisHi,
Welcome to the forum!
Whenever you have a question, you should post your sample data in a form that people can use to re-create the problem and thest their soluitons.
For example:
CREATE TABLE sup
( ACCOUNT_STATUS VARCHAR2 (10)
, BILLING_STATUS VARCHAR2 (10)
, SUPPRESSION_FLAG VARCHAR2 (10)
, BILLING_STATUS_TEXT VARCHAR2 (10)
INSERT INTO sup (ACCOUNT_STATUS, BILLING_STATUS, SUPPRESSION_FLAG, BILLING_STATUS_TEXT)
VALUES ('99999', 'BB', '99999', 'Unbill');
INSERT INTO sup (ACCOUNT_STATUS, BILLING_STATUS, SUPPRESSION_FLAG, BILLING_STATUS_TEXT)
VALUES ('99999', 'CA', '99999', 'Unbill');
INSERT INTO sup (ACCOUNT_STATUS, BILLING_STATUS, SUPPRESSION_FLAG, BILLING_STATUS_TEXT)
VALUES ('OK', 'CA', '99999', 'Bill');
INSERT INTO sup (ACCOUNT_STATUS, BILLING_STATUS, SUPPRESSION_FLAG, BILLING_STATUS_TEXT)
VALUES ('PE', 'CA', 'N', 'Unbill');
CREATE TABLE ast
( account_num NUMBER
, ACCOUNT_STATUS VARCHAR2 (10)
, BILLING_STATUS VARCHAR2 (10)
INSERT INTO ast (account_num, account_status, billing_status) VALUES (12356566, 'OK', 'CA');
INSERT INTO ast (account_num, account_status, billing_status) VALUES (96785674, 'AB', 'CA');
COMMIT;It also helps if you post the correct results clearly:
ACCOUNT_NUM BILLING_ST
12356566 Bill
96785674 UnbillSo the problem is, when you have more than one output row for the same account_num, you only want to display one of them.
You can do that by ranking the rows, using the analytic ROW_NUMBER function to assign numbers 1, 2, 3, ... to each row, with a different set of numbers for each account_num, and then pciking only the rows that were assigned number 1. (This is called a Top-N Query .)
What makes one row more desireable than another? You said, in the case of account_num=12356566, that it is the row where sup.account_status='OK'. Why? Is that because
(a) 'OK' is always better than 'PE' or '99999', or is it because
(b) 'OK' is also ast.account_status
? I'm guessing that it is (b), but if I'm wrong, you can change the analytic ORDER BY clause below:
WITH got_rnum AS
SELECT ast.account_num
, sup.billing_status_text
, ROW_NUMBER () OVER ( PARTITION BY ast.account_num
ORDER BY CASE
WHEN sup.account_status = ast.account_status
THEN 1
ELSE 2
END
) AS rnum
FROM ast
, sup
WHERE ( sup.account_status = ast.account_status
OR sup.account_status = '99999'
AND ast.billing_status = sup.billing_status
SELECT account_num
, billing_status_text
FROM got_rnum
WHERE rnum = 1
; The analytic ORDER BY clause is similar to the query ORDER BY clause in that, if there are two or more rows with the same values for the ORDER BY columns, there's no telling in what order those rows will appear. You can add more expressions to the ORDER BY clause as tie-breakers, if necessary. -
Error while using REMAP_TABLE and WHERE clause together in IMPDP
I am trying to move some records from a very large table to another small table.
I am facing trouble while using REMAP_TABLE and WHERE clause together in IMPDP.
Problem is data filter is not getting applied and all records are getting imported.
here is how I have simulated this. please advice.
CREATE TABLE TSHARRHB.TMP1
A NUMBER,
B NUMBER
begin
Insert into TSHARRHB.TMP1
(A, B)
Values
(1, 1);
Insert into TSHARRHB.TMP1
(A, B)
Values
(2, 2);
COMMIT;
end;
expdp system/password TABLES=tsharrhb.TMP1 DIRECTORY=GRDP_EXP_DIR DUMPFILE=TMP1.dmp REUSE_DUMPFILES=YES LOGFILE=EXP.log PARALLEL=8
impdp system/password DIRECTORY=GRDP_EXP_DIR DUMPFILE=TMP1.dmp LOGFILE=imp.log PARALLEL=8 QUERY='TSHARRHB.TMP1:"WHERE TMP1.A = 2"' REMAP_TABLE=TSHARRHB.TMP1:TMP3 CONTENT=DATA_ONLY
Import: Release 11.2.0.1.0 - Production on Fri Dec 13 05:13:30 2013
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/********@GRD6.RBSG DIRECTORY=GRDP_EXP_DIR DUMPFILE=TMP1.dmp LOGFILE=SSD_93_TABLES_FULL_EXP.log PARALLEL=8 QUERY=TSHARRHB.TMP1:"WHERE TMP1.A = 2" REMAP_TABLE=TSHARRHB.TMP1:TMP3 CONTENT=DATA_ONLY
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "TSHARRHB"."TMP3" 5.421 KB 2 rows
Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at 05:13:33
here I am expecting only 1 record to get imported but both the records are getting imported. please advice.The strange thing compared to your output is that I get an error when I have table prefix in the query block:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** DUMPFILE=TMP1.dmp LOGFILE=imp.log PARALLEL=8 QUERY=SYSADM.TMP1:"WHERE TMP1.A = 2" REMAP_TABLE=SYSADM.TMP1:TMP3 CONTENT=DATA_ONLY
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
ORA-31693: Table data object "SYSADM"."TMP3" failed to load/unload and is being skipped due to error:
ORA-38500: Unsupported operation: Oracle XML DB not present
Job "SYSTEM"."SYS_IMPORT_FULL_01" completed with 1 error(s) at Fri Dec 13 10:39:11 2013 elapsed 0 00:00:03
And if I remove it, it works:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** DUMPFILE=TMP1.dmp LOGFILE=imp.log PARALLEL=8 QUERY=SYSADM.TMP1:"WHERE A = 2" REMAP_TABLE=SYSADM.TMP1:TMP3 CONTENT=DATA_ONLY
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SYSADM"."TMP3" 5.406 KB 1 out of 2 rows
Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at Fri Dec 13 10:36:50 2013 elapsed 0 00:00:01
Nicolas.
PS: as you can see, I'm on 11.2.0.4, I do not have 11.2.0.1 that you seem to use. -
Using function in where clause
I have created a function as follows
create or replace FUNCTION get_codes RETURN varchar2 IS
scodes varchar2(50) := 'A1,A2';
BEGIN
scodes := '('''||REPLACE(scodes,',',''',''')||''')';
return scodes;
END;
this function returns ('A1','A2')
now i want to use this in where clause, both below statements fetches no rows
select * from tablea where code in (select get_codes from dual);
select * from tablea where code in get_codes;
but the following will fetch rows
select * from tablea where code in ('A1','A2')
how to use function in where clause
ThanksHi,
The code that works:
where code in ('A1','A2')is testing whther code is either of two 2-character strings, A1 or A2.
The ones that don't work, such as:
where code in get_codes;are comparing code to one 9-character string: 'A1','A2', where characters 1, 4, 6 and 9 are single-quotes. (The function can only return one value.)
You can use dynamic SQL to embed that 9-character string in part of your query, where it will be interpreted as two 2-character strrings.
[Oracle Base|http://www.oracle-base.com/articles/misc/DynamicInLists.php] and AskTom have good pages on the subject of dynamic IN-lists.
Of you can use INSTR or LIKE:
where INSTR ( get_codes
, '''' || code || ''''
) > 0 -
Can we use EXISTS in where condition
hi all,
can we use EXISTS (instead of IN) in where condition of a query which is not part of a subquery ?
for e.g:
update gpf_vou_sm set up_amt=0 where src_cd exists ('1101','1201');
the query works fine with the IN operator in place of EXISTS.
anybody please help.
thomas k prakashHi:
You can use EXISTS in where clause but not in that syntax.
UPDATE gpf_vou_sm g
SET up_amt = 0
WHERE EXISTS (SELECT 1
FROM gpf_vou_sm
WHERE src_cd IN ('1101', '1201') AND id = g.id);Using IN in your case is better...
Saad, -
Hello all,
I am new to Oracle, currently using 10G + aspvbscript.
I've been trying to query data using date in where clause but nothing seems to work.
The column is in date format.
It gets printed out like this: 5/1/2010 11:21:19 AM
I tried using this query:
SELECT * from table where TRUNC(user_date) > to_date('FEB-01-2010:00:00:00','mm-dd-yyyy:HH24:MI:SS') order by user_date asc.
It does return an output but it returns everything in table and does not take WHERE clause into consideration however, it does sort the date in ascending order.
I've tried getting rid of TRUNC tried to format date in a different way but no such luck.
Please point me to the right direction.
Thanks.Welcome to the forums!
In cases like this it is helpful if you can provide the following information:
1. Oracle version (SELECT * FROM V$VERSION)
2. Sample data in the form of CREATE / INSERT statements.
3. Expected output
4. Explanation of expected output (A.K.A. "business logic")
5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details.
I'll try and take a stab at your request based on the data given. What your query says is that it will return all rows that have a date greater then 2/1/2010 (MM/DD/YYYY). If your query is returning all rows then maybe the possibility exists that all the dates in the table are greater then 2/1/2010. Have you checked all dates to see if this is the case?
Also, one note about your TO_DATE() function.to_date('FEB-01-2010:00:00:00','mm-dd-yyyy:HH24:MI:SS')The date format does not match the string you are using with respect to month. Your string has 'FEB' but the format is 'MM' which is the numeric representation of the month. Although Oracle was able to convert it to the proper date on my system you should try and maintain consistency between the string and the date format used. -
Using distinct in where clause
Greedings,
I am trying to execute some kind of distinct using rowid in where clause but for some reason im not getting the appropriate result. Any guidance is welcome
SELECT acnt_code,cat,cat_desc,buc FROM so_budgets_cat where
Trim(buc)='S03'
and
NOT exists (SELECT 1
from so_budgets_cat tab2
where ( tab2.cat = so_budgets_cat.cat )
and tab2.ROWID > so_budgets_cat.ROWID
ORDER BY cat,so_budgets_cat.rowidThanks in advanceAdditionally to the fact that we don't have an idea on the expected output, I can't understand the comparison of ROWIDs in your query. These are the logical addresses where the data reside in the disks, comparing them doesn't make sense to me; Oracle DB defines where to store, based on space availability in the disk allocation to the database occupation - and NOT in a logical sequence.
Maybe the existing row is stored in an address that is not a higher number than other, and so it is not being retrieved in your sub-query - hence your test for distinction is failing. -
Can you use 'Home Share' where there are 2 itunes on the same computer, but under different user profiles?
check out method one from this support article -> How to use multiple iPods, iPads, or iPhones with one computer
Maybe you are looking for
-
How do I use the built in web cam?
I would like to start using the web cam on this TouchSmart 600 but I am not sure where to go or how to start. Do I need to use a certain software or how can I get to it? Thanks
-
Ankh SVN does not work with Oracle DB Project in ODT 11.1.0.5.10 beta
I was able to add the new Oracle Database Project to my Subversion repository using TortioseSVN, but unfortunately, the Ankh Source Control options do not appear within Visual Studio and none of the icon overlays appear within Visual Studio. Does any
-
How do I download the bible to my iPod touch?
How do I download the bible to my iPod Touch?
-
Creating a Distribution list for iPhone using Outlook
I use Microsoft outlook for contacts and have multiple "Distribution lists" that I send information to. The DL's are not syncing with my iPhone. I need to send group emails and am having to put each name in individually. Is there a way, using Outlook
-
I am on the evaluation of different technologies, I tried to get out the details of minimum hardware requirements for Developers' Desktop, Developement Server, Production Server, with regards to the J2EE. I could not find the same on any of the docum