Regarding decode function

Hi all,
i want to know abt decode function in oracle-sql
i..e passing the parameters in decode using decode using ":" bind parameter
select JOB, decode(:j,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP;
i declared the j variable in sql environment
VARIABLE J VARCHAR2(20);
i exec the query
and passed as the clerk as input parameter
but iam getting the o/p as
JOB DECODE(:J
MANAGER MANAGER
MANAGER MANAGER
MANAGER MANAGER
SALESMAN SALESMAN
SALESMAN SALESMAN
SALESMAN SALESMAN
CLERK CLERK
SALESMAN SALESMAN
ANALYST ANALYST
CLERK CLERK
ANALYST ANALYST
JOB DECODE(:J
CLERK CLERK
12 rows selected.
SQL> PRINT J;
J
im not getting o/p
so please hlelp me only : should be used not &
this is my first thread.
execuse me if any mistakes
tons of thanks in advance to all

SORRY DAVE,ROD FOR NOT BEING CLEAR,
IAM WORKING WITH EMP TABLE THE DATA IN IT IS LIKE THIS
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 chaitu SALESMAN 7698 20-FEB-81 1600 300 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
AND I USED DECODE FUNCTION
select JOB BEFOREPROMOTION, decode(job,'CLERK','MANAGER','ANALYST','EXEC') PROMOTION FROM EMP
BEFOREPRO PROMOTI
MANAGER
MANAGER
MANAGER
SALESMAN
SALESMAN
SALESMAN
CLERK MANAGER
SALESMAN
ANALYST EXEC
CLERK MANAGER
ANALYST EXEC
I..E ALL THE CLERKS TO MANAGERS AND SAME THING WITH ANALYST
OK
NOW I WILL SHOW U ANOTHER QUERY
select job,decode(job,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP where job=&job;
Enter value for job: 'CLERK'
old 1: select job,decode(job,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP where job=&job
new 1: select job,decode(job,'CLERK','MANAGER','ANALYST','EXEC',JOB) FROM EMP where job='CLERK'
JOB DECODE(JO
CLERK MANAGER
CLERK MANAGER
CLERK MANAGER
HERE OBSERVE THAT I PASSED JOB ='CLERKS AS PARAMETER AFTER EXECUTING THE QUERY
IT ASKED LIKE THIS AND SHOWED THE REPLACED VALUES
Enter value for job: 'CLERK'
AS SHOWN ABOVE
FOR THIS TO PASS VALUE
IN TO THE QUERY BY USING AMPERSAND('&')
TO TAKE INPUT AS CLERK
IT CHECKED AND RETURNED THE VALUE
IN THE SIMILAR WAY CAN I USE ":" COLON
TO PASS PARAMETERS
IN TO THE QUERY
CAN I USE IT
REGARDS,
PHANI
Edited by: user10652894 on Nov 26, 2008 3:24 AM

Similar Messages

  • Question regarding decode function.

    Hi friends,
    I have a question regarding using decode.
    I'm try'g to explain my problem using emp table.
    Can you guys please help me out.
    For example consider emp table, now i want to get all manager id's concatenated for 2 employees.
    I tried using following code
    declare
    v_mgr_code  number(10);
    v_mgr1      number(4);
    v_mgr2      number(4);
    begin
    select  mgr into    v_mgr1
    from    scott.emp
    where   empno = 7369;
    select  mgr into    v_mgr2
    from    scott.emp
    where   empno = 7499;
    select v_mgr1||'-'||v_mgr2 into v_mgr_code from dual;
    end;now instead of writing 2 select statements can i write one select statement using decode function ?
    Edited by: user642856 on Mar 8, 2009 11:18 PM

    i don't know wheter your looking for this or not.if i am wrong correct me.
    SELECT Ename||' '||initcap('manager is ')||
    DECODE(MGR,
            7566, (SELECT Ename
                    FROM Emp
                    WHERE Empno = 7566),
            7698, (SELECT Ename
                    FROM Emp
                    WHERE Empno = 7698),
            7782, (SELECT Ename
                    FROM Emp
                    WHERE Empno = 7782),
            7788, (SELECT Ename
                    FROM Emp
                    WHERE Empno = 7788),
            7839, (SELECT Ename
                    FROM Emp
                    WHERE Empno = 7839),
            7902, (SELECT Ename
                    FROM Emp
                    WHERE Empno = 7902),
            'Do Not Know')  Manager from empor
    SELECT Ename||' '||initcap('manager is ')||
    DECODE(MGR,
            7566, (SELECT empno
                    FROM Emp
                    WHERE Empno = 7566),
            7698, (SELECT empno
                    FROM Emp
                    WHERE Empno = 7698),
            7782, (SELECT empno
                    FROM Emp
                    WHERE Empno = 7782),
            7788, (SELECT empno
                    FROM Emp
                    WHERE Empno = 7788),
            7839, (SELECT empno
                    FROM Emp
                    WHERE Empno = 7839),
            7902, (SELECT empno
                    FROM Emp
                    WHERE Empno = 7902)) manager
    from empEdited by: user4587979 on Mar 8, 2009 9:52 PM

  • Logical Operations in SQL decode function ?

    Hi,
    Is it possible to do Logical Operations in SQL decode function
    like
    '>'
    '<'
    '>='
    '<='
    '<>'
    not in
    in
    not null
    is null
    eg...
    select col1 ,order_by,decode ( col1 , > 10 , 0 , 1)
    from tab;
    select col1 ,order_by,decode ( col1 , <> 10 , 0 , 1)
    from tab;
    select col1 ,order_by,decode ( col1 , not in (10,11,12) , 0 , 1)
    from tab;
    select col1 ,order_by,decode ( col1 ,is null , 0 , 1)
    from tab;
    Regards,
    infan
    Edited by: user780731 on Apr 30, 2009 12:07 AM
    Edited by: user780731 on Apr 30, 2009 12:07 AM
    Edited by: user780731 on Apr 30, 2009 12:08 AM
    Edited by: user780731 on Apr 30, 2009 12:08 AM
    Edited by: user780731 on Apr 30, 2009 12:09 AM

    example:
    select col1 ,order_by,case when col1 > 10 then 0 else 1 end
    from tab;
    select col1 ,order_by,case when col1 &lt;&gt; 10 then 0 else 1 end
    from tab;
    select col1 ,order_by,case when col1 not in (10,11,12) then 0 else 1 end
    from tab;As for testing for null, decode handles that by default anyway so you can have decode or case easily..
    select col1 ,order_by,decode (col1, null , 0 , 1)
    from tab;
    select col1 ,order_by,case when col1 is null then 0 else 1 end
    from tab;

  • Decode function

    I am using 10gR2.
    We can call SQL built-in functions such as SUBSTR, UPPER, LOWER in the RETURN clause of a function, but when we call a decode in there we get PLS-00204 error. How DECODE differs from other built-in functions?
    Thanks in advance
    Badri

    Hi,
    The function behavior is different if you compare SUBSTR / UPPER / LOWER with DECODE.
    DECODE functionality is similar to IF Statement, so we can not use it as an expression.
    In user defined functions, we can use expression in RETURN Clause.
    Moreover, we can not use all SQL Functions in PL/SQL with same method.
    Regards,
    Sailaja
    Edited by: Sailaja Pasupuleti on Dec 25, 2008 6:51 PM

  • Decode Function issue

    Hi all,
    i want compare two rows in a table. so i used decode function to compare the values, whether its matched or not.
    One of my table column has Long data type.
    Decode(a.col1,b.col1,'Match','NotMatch')
    col1 - is long data type.
    when i use long data type, i got below error,
    is there anyway to rectify this error.
    SQL Error: ORA-00932: inconsistent datatypes: expected NUMBER got LONG
    00932. 00000 - "inconsistent datatypes: expected %s got %s"
    Thanks in advance.
    Regards,
    Santhosh

    >
    You can't use ANY function on a LONG column. This restriction is not limited to DECODE.
    >
    Not quite correct.
    You can use TO_LOB on a LONG column but only in the select list of a subquery in an INSERT statement.
    See TO_LOB in the SQL Language doc
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions205.htm#SQLRF06134
    >
    TO_LOB converts LONG or LONG RAW values in the column long_column to LOB values. You can apply this function only to a LONG or LONG RAW column, and only in the select list of a subquery in an INSERT statement.
    Before using this function, you must create a LOB column to receive the converted LONG values. To convert LONG values, create a CLOB column. To convert LONG RAW values, create a BLOB column.
    You cannot use the TO_LOB function to convert a LONG column to a LOB column in the subquery of a CREATE TABLE ... AS SELECT statement if you are creating an index-organized table. Instead, create the index-organized table without the LONG column, and then use the TO_LOB function in an INSERT ... AS SELECT statement.
    You cannot use this function within a PL/SQL package. Instead use the TO_CLOB or TO_BLOB functions.

  • SQL*Loader and DECODE function

    Hi All,
    I am loading data from data files into oracle tables and while loading the data using SQL*Loader, the following requirement needs to be fulfilled.
    1) If OQPR < 300, RB = $ 0-299, SC = "SC1"
    2) If 300 < OQPR < 1200, RB = $ 300-1199, SC = "SC2"
    3) If 1200 < OQPR < 3000, RB = $ 1200-2999, SC = "SC3"
    4) If OQPR > 3000 USD, RB = > $3000, SC = "SC4"
    Here OPQR is a field in the data file.
    Can anyone suggest how do we handle this using DECODE function? Triggers and PL/SQL functions are not to be used.
    TIA.
    Regards,
    Ravi.

    The following expression gives you different values for your different intervals and boundaries :
    SIGN(:OQPR - 300) + SIGN(:OQPR - 1200) + SIGN(:OQPR - 3000)

  • DECODE function in Oracle gives duplicate data

    Hi,
    I want to retrieve distinct result using decode function and given below are the queries for that.
    SELECT DISTINCT O.*, DECODE(TA,'AP',AMT,'AC1',AMT,'AC2',AMT,'AC3',AMT,'AC4',AMT,NULL) AS CA,
    DECODE(TA,'AD1',AMT,NULL) AS DA FROM HT O, HIS H, V_CV
    WHERE O.HID=H.HID AND H.HTYP='TRN' AND O.HID = V.HID AND V.CID = '5' ORDER BY CA ASC;
    which gives me
    HID   TA      PT     AMT     CA         DA
    2     A     V     4.1     4.1     null
    3     C     V     14.02 14.02     null
    1     D     V 5.1     null          5.1
    4     D     V     6.21     null      6.21
    which is correct but i want to do sorting on two different column which do not exit in the database and they are dynamic.So i Modified the Query but i am getting duplicate results
    SELECT DISTINCT O.*,
    DECODE(o.TA,cr.TA,o.AMT,NULL) AS CA,
    DECODE(o.TA,de.TA,o.AMT,NULL) AS DA
    FROM HT O,
    HIS H,
    V_CV,
    (select TA from RTD where Ttype= 'C') cr,
    (select TA from RTD where Ttype= 'D') de
    WHERE
    O.HID=H.HID
    AND H.HTYP='TRN'
    AND O.HID = V.HID
    AND V.CID = '5'
    ORDER BY CA ASC
    which gives me
    HID   TA      PT     AMT     CA         DA
    2     A     V     4.1     4.1     null
    3     C     V     14.02 14.02     null
    3     C     V     14.02 null      null
    1     D     V 5.1     null          5.1
    1     D     V 5.1     null          null
    4     D     V     6.21     null      6.21
    2     A     V     4.1     null     null
    which is obviously giving HID 2,3,1 as duplicate value since DECODE select a value and compares with all item where i want only single comparison.
    Any help regarding this would be appreciated.
    Edited by: Pawanchoure on Jul 5, 2011 7:14 AM

    Why don't you have any join conditions specified for your two inline views cr and de?
    If you want help with a query you should post DDL and inserts statements to create enough test data for someone to produce the set of results that you are looking for.
    HTH -- Mark D Powell --

  • How to use a decode function

    I want to use the following with decode, recently started on BODS.
    Can you please provide the steps to use decode function
    in source tabke fieldname is ordid and in target fieldname is Orderid
    decode(Order_ID = 1709,lpad(Order_ID,7,'700'),Order_ID)
    thank you very much for the helpful i

    1) In the mapping tab of target column, click on Functions, select Miscellaneous Functions category, you can find decode function in it. Lpad can be found in String functions category.
    OR
    2) You can directly script this or paste this code at the mapping tab. In this case, remove the Order_ID from the script wherever used, drag and drop this column from the input schema of Query transform to avoid the syntax error. 
    Regards,
    Suneer

  • How to write decode function in plsql

    hi
    guys
    can u plz help me in this topic
    .........

    Hi ,
    You could use the decode function in an SQL statement as follows:
    SELECT supplier_name,
    decode(supplier_id,      10000,      'IBM',
         10001,      'Microsoft',
         10002,      'Hewlett Packard',
              'Gateway') result
    FROM suppliers;
    The above decode statement is equivalent to the following IF-THEN-ELSE statement:
    IF supplier_id = 10000 THEN
    result := 'IBM';
    ELSIF supplier_id = 10001 THEN
    result := 'Microsoft';
    ELSIF supplier_id = 10002 THEN
    result := 'Hewlett Packard';
    ELSE
    result := 'Gateway';
    END IF;
    The decode function will compare each supplier_id value, one by one.
    Regards,
    xaheer

  • Using decode function without negative values

    Hi friends
    I am using oracle 11g
    I have at doubt regarding the following.
    create table Device(Did char(20),Dname char(20),Datetime char(40),Val char(20));
    insert into Device values('1','ABC','06/13/2012 18:00','400');
    insert into Device values('1','abc','06/13/2012 18:05','600');
    insert into Device values('1','abc','06/13/2012 18:55','600');
    insert into Device values('1','abc','06/13/2012 19:00','-32768');
    insert into Device values('1','abc','06/13/2012 19:05','800');
    insert into Device values('1','abc','06/13/2012 19:10','600');
    insert into Device values('1','abc','06/13/2012 19:15','900');
    insert into Device values('1','abc','06/13/2012 19:55','1100');
    insert into Device values('1','abc','06/13/2012 20:00','-32768');
    insert into Device values('1','abc','06/13/2012 20:05','-32768');
    Like this I am inserting data into table for every 5 minutes Here i need the result like
    output:
    Dname 18:00 19:00 20:00
    abc 400 -32768 -32768
    to retrieve this result i am using decode function
    SELECT Dname,
    MAX(DECODE ( rn , 1,val )) h1,
    MAX(DECODE ( rn , 2, val )) h2,
    FROM
    (SELECT Dname,Datetime,row_number() OVER
    (partition by Dname order by datetime asc) rn FROM Device
    where substr(datetime,15,2)='00' group by Dname.
    According to above data expected result is
    Dname 18:00 19:00 20:00
    abc 400 600(or)800 1100
    This means I dont want to display negative values instead of that values i want to show previous or next value.
    Edited by: 913672 on Jul 2, 2012 3:44 AM

    Are you looking for something like this?
    select * from
    select dname,
           datetime,
           val,
           lag(val) over (partition by upper(dname) order by datetime) prev_val,
           lead(val) over (partition by upper(dname) order by datetime) next_val,
           case when nvl(val,0)<0  and lag(val) over (partition by upper(dname) order by datetime) >0 then
             lag(val) over (partition by upper(dname) order by datetime)
           else 
             lead(val) over (partition by upper(dname) order by datetime)
           end gt0_val
    from device
    order by datetime
    where substr(datetime,15,2)='00';Please take a look at the result_column gt0_val.
    Edited by: hm on 02.07.2012 04:06

  • Please help in correcting the Decode function

    DECODE(p_resp_key,'||'''AP'''||',LINT_LOGI_STG_ITEM_DETAILS_PKG.GET_AP_ITEM_CATEGORY_DTL(ol.inventory_item_id),ldmf_istore_order_pkg.get_wwdb_prod_description(ol.inventory_item_id)) AS description
    I am getting parsing errors during runtime.I believe that something is incorrect in Decode statement.I am calling this Decopde statement in the package.Please help.
    Regards
    M

    Thanks Sven for replying i am pasting the full function kindly tell me which will be the proper manner to correct teh decode statement.Please look for "--CO73977 iStore product description change start by manish" in the belwo defintion there i have used decode function.Kindly help.
    v_statement := 'SELECT /*+ FIRST_ROWS */ DISTINCT ooh.order_number AS "Order Number",
    NVL(bill_cas.attribute5,hp.party_name) AS "Customer Name",
    TO_CHAR(ooh.ordered_date,'||'''DD-MON-YYYY'''||') AS "Order Date",
    TO_CHAR(ooh.booked_date,'||'''DD-MON-YYYY'''||') AS "Booked Date",
    TO_CHAR(ol.request_date,'||'''DD-MON-YYYY'''||') AS "Requested Date",
    oel.meaning AS "Order Status",
    ooh.cust_po_number AS "PO Number",
    ooh.transactional_curr_code AS currency,
    terms.NAME AS "Payment Terms",
    freight.meaning AS "Freight Term",
    fob.meaning AS "FOB",
    ooh.sales_channel_code AS "Sales Channel",
    bill_to.LOCATION AS "Bill To Location",
    ship_to.LOCATION AS "Ship To Location",
    terr.segment1 AS "Sales Territory - Country",
    ott.NAME AS "Order Type",
    oe_totals_grp.get_order_total (ooh.header_id,NULL,'||'''ALL'''||')* DECODE (ooh.order_category_code, '||'''RETURN'''||', -1, 1) AS "Order Total",
    CONCAT(CONCAT(ol.line_number,'||'''.'''||'),ol.shipment_number) AS "Line Number",
    ol.ordered_item AS "Item Number",
    ldmf_get_customer_item(ol.sold_to_org_id,ol.inventory_item_id,ol.invoice_to_org_id,ol.ship_to_org_id) AS "Customer SKU",
    --ldmf_istore_order_pkg.get_wwdb_prod_description(ol.inventory_item_id) AS description,
                             --CO73977 iStore product description change start by manish
                             --DECODE(p_resp_key,'||'''AP'''||',LINT_LOGI_STG_ITEM_DETAILS_PKG.GET_AP_ITEM_CATEGORY_DTL(ol.inventory_item_id),ldmf_istore_order_pkg.get_wwdb_prod_description(ol.inventory_item_id)) AS description,
              --CO73977 iStore product description change end by manish
                             LINT_LOGI_STG_ITEM_DETAILS_PKG.GET_AP_ITEM_CATEGORY_DTL(ol.inventory_item_id) AS description,
    ol.ordered_quantity AS "Ordered Quantity",
    ol.shipped_quantity AS "Shipped Quantity",
    TO_CHAR(ol.schedule_ship_date,'||'''DD-MON-YYYY'''||') AS "Schedule Ship Date",
    ol.unit_selling_price AS "Unit Price",
    ol.ordered_quantity * ol.unit_selling_price AS "Extended Amount",
    ol.tax_value "taxes_total",
    ldmf_istore_order_pkg.get_freight_charges(ooh.header_id,ol.line_id) AS "Freight Charges",
    ldmf_istore_order_pkg.get_pallet_charges (ooh.header_id,ol.line_id) AS "Pallet Surcharge",
    oe_oe_totals_summary.line_charges (ooh.header_id, ol.line_id ) AS "Total Charges",
    DECODE(oell.meaning,'||'''Awaiting Shipping'''||','||'DECODE(TO_CHAR(LDMF_ISTORE_ORDER_PKG.CHK_DELIVERY_LN_EXISTS(ol.header_id,ol.line_id)),'||
                                                                                                                  '''1'''||','||'''Picked'''||','||
                                                                                                                       '''2'''||','||'''Picked Partial'''||','||'oell.meaning)
                             ,oell.meaning) AS "Line Status",
    TO_CHAR(ol.actual_shipment_date,'||'''DD-MON-YYYY'''||') AS "Ship Date",
    (SELECT ware_house.NAME
    FROM hr_all_organization_units_tl ware_house
    WHERE ware_house.organization_id = NVL(ol.ship_from_org_id,ooh.ship_from_org_id)) AS WAREHOUSES,
    LDMF_ISTORE_ORDER_PKG.GET_TRACKING_NUMBER(ol.header_id,ol.line_id) AS "Tracking Number",
    LDMF_ISTORE_ORDER_PKG.GET_WAYBILL_NUMBER(ol.header_id,ol.line_id) AS "Waybill Number",
    LDMF_ISTORE_ORDER_PKG.GET_DELIVERY_NUMBER(ol.header_id,ol.line_id) delivery_number,
    LDMF_ISTORE_ORDER_PKG.GET_PRO_NUMBER(ol.header_id,ol.line_id) AS "Pro Number",
    LDMF_ISTORE_SALESREP_PKG.LDMF_ISTORE_CHECK_HOLDS(hcaa.cust_account_id,ooh.header_id,ol.line_id) AS "Hold Applied",
    LDMF_ISTORE_ORDER_PKG.GET_CONV_RATE(ol.ordered_item_id,ooh.org_id) AS "Pallet Qty",
    ROUND(ol.ordered_quantity/DECODE(LDMF_ISTORE_ORDER_PKG.GET_CONV_RATE(ol.ordered_item_id,ooh.org_id),0,1,LDMF_ISTORE_ORDER_PKG.GET_CONV_RATE(ol.ordered_item_id,ooh.org_id)),2) AS "Pallet#",
    ldmf_istore_order_pkg.get_invoice_number(ooh.order_number,ol.line_id) AS "Invoice Number",
    ldmf_istore_salesrep_pkg.get_promonum(ooh.header_id,ol.line_id) AS "Promo Number",
    ol.line_number,ol.shipment_number,ooh.header_id,ooh.ordered_date,
    LDMF_ISTORE_ORDER_PKG.GET_SHIP_METHOD_MEANING(ol.header_id,ol.line_id) AS SHIP_METHOD_CODE,
    terms.description AS "Payment_Term_Desc",
    TO_CHAR((LDMF_ISTORE_ORDER_PKG.GET_ESTI_ARR_DATE(ol.ACTUAL_SHIPMENT_DATE,
                             ol.SHIP_FROM_ORG_ID,
                             ol.SHIP_TO_ORG_ID,
                             ol.SHIPPING_METHOD_CODE )),'||'''DD-MON-YYYY'''||') AS "Arrival_Date",
                             DECODE (LDMF_ISTORE_ORDER_PKG.GET_ORD_AMDMT_STATUS(ooh.header_id,ol.line_id),1 ,'||'''YES'''||',NULL) AS "Pending_Approval"
    FROM oe_order_headers_all ooh,
    oe_order_lines_all ol,
    hz_cust_accounts_all hcaa,
    oe_transaction_types_all otta,
    oe_transaction_types_tl ott,
    hz_parties hp,
    hz_party_sites hps,
    oe_lookups oel,
    oe_lookups oell,
    ra_terms terms,
    fnd_lookup_values freight,
    fnd_lookup_values fob,
    wsh_new_deliveries dl,
    wsh_delivery_details dd,
    wsh_delivery_assignments da,
    oe_order_holds_all holds,
    hz_cust_site_uses_all bill_to,
    hz_cust_acct_sites_all bill_cas,
    hz_cust_site_uses_all ship_to,
    hz_cust_acct_sites_all ship_cas,
    hz_locations loc,
    ra_territories terr,
    org_organization_definitions ware_house,
    wsh_carrier_services wcs
    WHERE ooh.header_id = ol.header_id
    AND ooh.sold_to_org_id = hcaa.cust_account_id
    AND wcs.ship_method_code(+) = ol.shipping_method_code
    AND hcaa.party_id = hp.party_id
    AND hps.party_id = hp.party_id
    AND loc.location_id = hps.location_id
    AND ship_cas.party_site_id = hps.party_site_id
    AND ooh.order_type_id = otta.transaction_type_id
    AND ooh.order_type_id = ott.transaction_type_id
    AND terms.term_id(+) = ooh.payment_term_id
    AND da.delivery_id = dl.delivery_id (+)
    AND dd.delivery_detail_id = da.delivery_detail_id(+)
    AND ship_to.cust_acct_site_id = ship_cas.cust_acct_site_id
    AND ship_to.site_use_id = ooh.ship_to_org_id
    AND ware_house.organization_id(+) = ol.ship_from_org_id
    AND bill_to.cust_acct_site_id = bill_cas.cust_acct_site_id
    AND bill_to.site_use_id = ooh.invoice_to_org_id
    AND bill_to.territory_id = terr.territory_id
    AND ol.header_id = dd.source_header_id(+)
    AND ol.line_id = dd.source_line_id(+)
    AND ol.line_id = holds.line_id(+)
    AND freight.lookup_type = '||'''FREIGHT_TERMS'''||'
    AND freight.LANGUAGE =USERENV('||'''LANG'''||')
    AND freight.lookup_code = ooh.freight_terms_code
    AND ott.LANGUAGE = USERENV('||'''LANG'''||')
    AND fob.lookup_code = ooh.fob_point_code
    AND fob.lookup_type = '||'''FOB'''||'
    AND fob.LANGUAGE =USERENV('||'''LANG'''||')
    AND NVL(otta.attribute10,'||'''Y'''||') <> '||'''N'''||'
    AND oel.lookup_code = ooh.flow_status_code
    AND oel.lookup_type = '||'''FLOW_STATUS'''||'
    AND oell.lookup_code = ol.flow_status_code
    AND oell.lookup_type = '||'''LINE_FLOW_STATUS'''||'
    AND ol.line_category_code <> '||'''RETURN'''||'
    AND ooh.order_category_code IN ('||'''ORDER'''||','||'''MIXED'''||')
    AND ooh.sold_to_org_id = '||p_sold_to_org_id
    || ' AND TRUNC(ooh.ordered_date) >= TRUNC(SYSDATE - '||p_no_of_days||')';
    v_statement := v_statement || ' ORDER BY ooh.ordered_date DESC,ooh.header_id,ol.line_number,ol.shipment_number ';
    OPEN p_data FOR v_statement;
    EXCEPTION
    WHEN OTHERS THEN
    errbuf := SQLERRM ;
    retcode := SQLCODE ;
    END ORDER_TRACKER_DAYS;
    --This Procedure fetches the Order details between the given from date and to date.
    PROCEDURE ORDER_TRACKER_BETWEEN_DAYS( p_sold_to_org_id IN NUMBER
    ,p_search_by IN VARCHAR2
    ,p_start_date IN DATE
    ,p_end_date IN DATE
                             ,p_resp_key IN VARCHAR2
    ,p_data OUT shop_cartdownload
    ,errbuf OUT VARCHAR2
    ,retcode OUT NUMBER
    ) AS
    v_start_date VARCHAR2(1000);
    v_end_date VARCHAR2(1000);
    v_search_by VARCHAR2(1000);
    BEGIN
    SELECT ''''||p_start_date||'''' INTO v_start_date FROM dual;
    SELECT ''''||p_end_date||'''' INTO v_end_date FROM dual;
    v_statement := NULL;
    v_statement := 'SELECT /*+ FIRST_ROWS */ DISTINCT ooh.order_number AS "Order Number",
    NVL(bill_cas.attribute5,hp.party_name) AS "Customer Name",
    TO_CHAR(ooh.ordered_date,'||'''DD-MON-YYYY'''||') AS "Order Date",
    TO_CHAR(ooh.booked_date,'||'''DD-MON-YYYY'''||') AS "Booked Date",
    TO_CHAR(ol.request_date,'||'''DD-MON-YYYY'''||') AS "Requested Date",
    oel.meaning AS "Order Status",
    ooh.cust_po_number AS "PO Number",
    ooh.transactional_curr_code AS currency,
    terms.NAME AS "Payment Terms",
    freight.meaning AS "Freight Term",
    fob.meaning AS "FOB",
    ooh.sales_channel_code AS "Sales Channel",
    bill_to.LOCATION AS "Bill To Location",
    ship_to.LOCATION AS "Ship To Location",
    terr.segment1 AS "Sales Territory - Country",
    ott.NAME AS "Order Type",
    oe_totals_grp.get_order_total (ooh.header_id,NULL,'||'''ALL'''||')* DECODE (ooh.order_category_code, '||'''RETURN'''||', -1, 1) AS "Order Total",
    CONCAT(CONCAT(ol.line_number,'||'''.'''||'),ol.shipment_number) AS "Line Number",
    ol.ordered_item AS "Item Number",
    ldmf_get_customer_item(ol.sold_to_org_id,ol.inventory_item_id,ol.invoice_to_org_id,ol.ship_to_org_id) AS "Customer SKU",
    --ldmf_istore_order_pkg.get_wwdb_prod_description(ol.inventory_item_id) AS description,
                             --CO73977 iStore product description change start by manish
                             DECODE(p_resp_key,'||'''AP'''||',LINT_LOGI_STG_ITEM_DETAILS_PKG.GET_AP_ITEM_CATEGORY_DTL(ol.inventory_item_id),ldmf_istore_order_pkg.get_wwdb_prod_description(ol.inventory_item_id)) AS description,
              --CO73977 iStore product description change end by manish
    ol.ordered_quantity AS "Ordered Quantity",
    ol.shipped_quantity AS "Shipped Quantity",
    TO_CHAR(ol.schedule_ship_date,'||'''DD-MON-YYYY'''||') AS "Schedule Ship Date",
    ol.unit_selling_price AS "Unit Price",
    ol.ordered_quantity * ol.unit_selling_price AS "Extended Amount",
    ol.tax_value "taxes_total",
    ldmf_istore_order_pkg.get_freight_charges(ooh.header_id,ol.line_id) AS "Freight Charges",
    ldmf_istore_order_pkg.get_pallet_charges (ooh.header_id,ol.line_id) AS "Pallet Surcharge",
    oe_oe_totals_summary.line_charges (ooh.header_id, ol.line_id ) AS "Total Charges",
    DECODE(oell.meaning,'||'''Awaiting Shipping'''||','||'DECODE(TO_CHAR(LDMF_ISTORE_ORDER_PKG.CHK_DELIVERY_LN_EXISTS(ol.header_id,ol.line_id)),'||
                                                                                         '''1'''||','||'''Picked'''||','||
                                                                                              '''2'''||','||'''Picked Partial'''||','||'oell.meaning)
                             ,oell.meaning) AS "Line Status",
    TO_CHAR(ol.actual_shipment_date,'||'''DD-MON-YYYY'''||') AS "Ship Date",
    (SELECT ware_house.NAME
    FROM hr_all_organization_units_tl ware_house
    WHERE ware_house.organization_id = NVL(ol.ship_from_org_id,ooh.ship_from_org_id)) AS WAREHOUSES,
    LDMF_ISTORE_ORDER_PKG.GET_TRACKING_NUMBER(ol.header_id,ol.line_id) AS "Tracking Number",
    LDMF_ISTORE_ORDER_PKG.GET_WAYBILL_NUMBER(ol.header_id,ol.line_id) AS "Waybill Number",
    LDMF_ISTORE_ORDER_PKG.GET_DELIVERY_NUMBER(ol.header_id,ol.line_id) delivery_number,
    LDMF_ISTORE_ORDER_PKG.GET_PRO_NUMBER(ol.header_id,ol.line_id) AS "Pro Number",
    LDMF_ISTORE_SALESREP_PKG.LDMF_ISTORE_CHECK_HOLDS(hcaa.cust_account_id,ooh.header_id,ol.line_id) AS "Hold Applied",
    LDMF_ISTORE_ORDER_PKG.GET_CONV_RATE(ol.ordered_item_id,ooh.org_id) AS "Pallet Qty",
    ROUND(ol.ordered_quantity/DECODE(LDMF_ISTORE_ORDER_PKG.GET_CONV_RATE(ol.ordered_item_id,ooh.org_id),0,1,LDMF_ISTORE_ORDER_PKG.GET_CONV_RATE(ol.ordered_item_id,ooh.org_id)),2) AS "Pallet#",
    ldmf_istore_order_pkg.get_invoice_number(ooh.order_number,ol.line_id) AS "Invoice Number",
    ldmf_istore_salesrep_pkg.get_promonum(ooh.header_id,ol.line_id) AS "Promo Number",
    ol.line_number,ol.shipment_number,ooh.header_id,ooh.ordered_date,
    LDMF_ISTORE_ORDER_PKG.GET_SHIP_METHOD_MEANING(ol.header_id,ol.line_id) AS SHIP_METHOD_CODE,
    terms.description AS "Payment_Term_Desc",
    TO_CHAR((LDMF_ISTORE_ORDER_PKG.GET_ESTI_ARR_DATE(ol.ACTUAL_SHIPMENT_DATE,
                             ol.SHIP_FROM_ORG_ID,
                             ol.SHIP_TO_ORG_ID,
                             ol.SHIPPING_METHOD_CODE )),'||'''DD-MON-YYYY'''||') AS "Arrival_Date",
                             DECODE (LDMF_ISTORE_ORDER_PKG.GET_ORD_AMDMT_STATUS(ooh.header_id,ol.line_id),1 ,'||'''YES'''||',NULL) AS "Pending_Approval"
    FROM oe_order_headers_all ooh,
    oe_order_lines_all ol,
    hz_cust_accounts_all hcaa,
    oe_transaction_types_all otta,
    oe_transaction_types_tl ott,
    hz_parties hp,
    hz_party_sites hps,
    oe_lookups oel,
    oe_lookups oell,
    ra_terms terms,
    fnd_lookup_values freight,
    fnd_lookup_values fob,
    wsh_new_deliveries dl,
    wsh_delivery_details dd,
    wsh_delivery_assignments da,
    oe_order_holds_all holds,
    hz_cust_site_uses_all bill_to,
    hz_cust_acct_sites_all bill_cas,
    hz_cust_site_uses_all ship_to,
    hz_cust_acct_sites_all ship_cas,
    hz_locations loc,
    ra_territories terr,
    org_organization_definitions ware_house,
    wsh_carrier_services wcs
    WHERE ooh.header_id = ol.header_id
    AND ooh.sold_to_org_id = hcaa.cust_account_id
    AND wcs.ship_method_code(+) = ol.shipping_method_code
    AND hcaa.party_id = hp.party_id
    AND hps.party_id = hp.party_id
    AND loc.location_id = hps.location_id
    AND ship_cas.party_site_id = hps.party_site_id
    AND ooh.order_type_id = otta.transaction_type_id
    AND ooh.order_type_id = ott.transaction_type_id
    AND terms.term_id(+) = ooh.payment_term_id
    AND da.delivery_id = dl.delivery_id (+)
    AND dd.delivery_detail_id = da.delivery_detail_id(+)
    AND ship_to.cust_acct_site_id = ship_cas.cust_acct_site_id
    AND ship_to.site_use_id = ooh.ship_to_org_id
    AND ware_house.organization_id(+) = ol.ship_from_org_id
    AND bill_to.cust_acct_site_id = bill_cas.cust_acct_site_id
    AND bill_to.site_use_id = ooh.invoice_to_org_id
    AND bill_to.territory_id = terr.territory_id
    AND ol.header_id = dd.source_header_id(+)
    AND ol.line_id = dd.source_line_id(+)
    AND ol.line_id = holds.line_id(+)
    AND freight.lookup_type = '||'''FREIGHT_TERMS'''||'
    AND freight.LANGUAGE =USERENV('||'''LANG'''||')
    AND freight.lookup_code = ooh.freight_terms_code
    AND ott.LANGUAGE = USERENV('||'''LANG'''||')
    AND fob.lookup_code = ooh.fob_point_code
    AND fob.lookup_type = '||'''FOB'''||'
    AND fob.LANGUAGE =USERENV('||'''LANG'''||')
    AND NVL(otta.attribute10,'||'''Y'''||') <> '||'''N'''||'
    AND oel.lookup_code = ooh.flow_status_code
    AND oel.lookup_type = '||'''FLOW_STATUS'''||'
    AND ol.line_category_code <> '||'''RETURN'''||'
    AND ooh.order_category_code IN ('||'''ORDER'''||','||'''MIXED'''||')
    AND oell.lookup_code = ol.flow_status_code
    AND oell.lookup_type = '||'''LINE_FLOW_STATUS'''||'
    AND ooh.sold_to_org_id = '||p_sold_to_org_id ;
    IF p_search_by = '1' THEN
    v_search_by := ' AND TRUNC(ooh.ordered_date) ';
    ELSIF p_search_by = '2' THEN
    v_search_by := ' AND TRUNC(ol.request_date) ';
    ELSIF p_search_by = '3' THEN
    v_search_by := ' AND TRUNC(ol.actual_shipment_date) ';
    ELSIF p_search_by = '4' THEN
    v_search_by := ' AND TRUNC(ol.schedule_ship_date) ';
    END IF;
    v_statement := v_statement || v_search_by;
    v_statement := v_statement ||' >= '|| v_start_date || v_search_by ||' <= '||v_end_date;
    v_statement := v_statement || ' ORDER BY ooh.ordered_date DESC,ooh.header_id,ol.line_number,ol.shipment_number ';
    OPEN p_data FOR v_statement;
    EXCEPTION
    WHEN OTHERS THEN
    errbuf := SQLERRM ;
    retcode := SQLCODE ;
    END ORDER_TRACKER_BETWEEN_DAYS;

  • 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 Prakash

    say
    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

  • SQL Expression in decode function or case statement?

    Can I put SQL expressions in decode function or case statement?
    For example,
    select le.profile, decode( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile, 0, 'N', 'Y')
    from element le;
    or
    select le.profile, case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0 THEN 'N'
    ELSE 'Y'
    from element le;
    None of the above work.
    Can anyone tell me how to make it work?
    Is there any workaround?
    Thanks,
    J

    You simply needed and END to your CASE statement;
    SQL> with profile_data as (
       select 'XXXX_AFTER' name, 1 object_id from dual),
         element as (
       select 1 profile from dual union all
       select 2 from dual)
    select le.profile,
       case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0
       THEN 'N'
       ELSE 'Y'
       END new_col
    from element le
       PROFILE N
             1 Y
             2 N

  • SQL DECODE function

    Oracle version: 9.2.0.1.0
    Procob version: 9.2.0.1.0
    Cobol version: 3.1.11
    O.S. version: Windows XP
    We have a pro*cobol file with the next conversion rule into a select defined with a cursor.
    NVL(TO_CHAR(A.F_ALTA,'YYYYMMDD'),' '),     
    NVL(TO_CHAR(A.F_ESTADO,'YYYYMMDD'),' '),
    we store the result data through a fetch of the cursor into two variables (W-F-ALTA and W-F-ESTADO) When the program executes an insert of these two values in a target table, we define the next conversion rule:
    TO_DATE(DECODE(:W-F-ALTA,' ',NULL,:W-F-ALTA),'YYYYMMDD'),
    TO_DATE(DECODE(:W-F-ESTADO,' ',NULL,:W-F-ESTADO),'YYYYMMDD')
    Pro*Cobol and Cobol does not have errors when we precompile and compile the program but, when we execute the generated exe file, Oracle displays the next error:
    ORA-01841 (full) year must be between -4713 and +9999, and not be 0
    We think that the DECODE function is not understood by the cobol or pro*cobol interpreter. If we execute the same code without the DECODE function (only with TO_DATE function) there is no errors in the execution of the program
    somebody knows if there are reported problems with this DECODE function in Pro*Cobol or Cobol and how to solve them?
    Thanks

    TO_DATE(DECODE(:W-F-ALTA,' ',NULL,:W-F-ALTA),'YYYYMMDD'),
    TO_DATE(DECODE(:W-F-ESTADO,' ',NULL,:W-F-ESTADO),'YYYYMMDD')The problem is with the input values of :W-F-ALTA and :W-F-ESTADO. Using the first line as an example, what you are saying is if :W-F-ALTA is ' ', substitute it with NULL, any other value should be :W-F-ALTA, then output the TO_DATE value of whatever the result is.
    So if the value of :W-F-ALTA is the wrong charater or in the wrong input format, TO_DATE function does not understand it.
    See examples of success and failures below with your Bind variables substituted with typical values.
    SQL> select TO_DATE(DECODE(' ',' ',NULL,'20060830'),'YYYYMMDD') from dual;
    TO_DATE(D
    SQL> select TO_DATE(DECODE('1999-01-01',' ','NULL','20060830'),'YYYYMMDD') from dual;
    TO_DATE(D
    30-AUG-06
    SQL> select TO_DATE(DECODE('SHOULD_ERROR',' ','NULL','SHOULD_ERROR'),'YYYYMMDD') from dual;
    select TO_DATE(DECODE('SHOULD_ERROR',' ','NULL','SHOULD_ERROR'),'YYYYMMDD') from dual
    ERROR at line 1:
    ORA-01841: (full) year must be between -4713 and +9999, and not be 0
    SQL> select TO_DATE(DECODE('20060830',' ','NULL','20060830'),'YYYYMMDD') from dual;
    TO_DATE(D
    30-AUG-06
    SQL> select TO_DATE(DECODE('12345678',' ','NULL','12345678'),'YYYYMMDD') from dual;
    select TO_DATE(DECODE('12345678',' ','NULL','12345678'),'YYYYMMDD') from dual
    ERROR at line 1:
    ORA-01843: not a valid month
    SQL>

  • Problem using DECODE() function with a Query of Queries

    I
    posted
    on my blog about an issue I was having trying to use the PL/SQL
    DECODE() function with a Coldfusion Query of Queries. This function
    works fine when you query a database for information. However, when
    you query another query, it seems that CF doesn't recognize it. I
    got errors stating that it found a left parenthesis where it
    expected a FROM key word. Here is a simplified version of what I am
    trying to do:
    quote:
    <!--- Simulated query; similar to what I was calling from
    my database --->
    <cfscript>
    qOriginal = queryNew("Name,Email,CountryCode",
    "VarChar,VarChar,VarChar");
    newRow = queryAddRow(qOriginal, 5);
    querySetCell(qOriginal, "Name", "Joe", 1);
    querySetCell(qOriginal, "Email", "[email protected]", 1);
    querySetCell(qOriginal, "CountryCode", "AMER", 1);
    querySetCell(qOriginal, "Name", "Sally", 2);
    querySetCell(qOriginal, "Email", "[email protected]", 2);
    querySetCell(qOriginal, "CountryCode", "AMER", 2);
    querySetCell(qOriginal, "Name", "Bob", 3);
    querySetCell(qOriginal, "Email", "[email protected]", 3);
    querySetCell(qOriginal, "CountryCode", "ASIA", 3);
    querySetCell(qOriginal, "Name", "Mary", 4);
    querySetCell(qOriginal, "Email", "[email protected]", 4);
    querySetCell(qOriginal, "CountryCode", "EURO", 4);
    querySetCell(qOriginal, "Name", "John", 5);
    querySetCell(qOriginal, "Email", "[email protected]", 5);
    querySetCell(qOriginal, "CountryCode", "EURO", 5);
    </cfscript>
    <cfquery name="qCountries" dbtype="query">
    SELECT DISTINCT(CountryCode) AS CountryCode,
    DECODE(states, "AMER", "North America &amp; Canada",
    "EURO", "Europe &amp; Africa", "ASIA", "Japan &amp;
    Asia","") CountryName
    FROM qOriginal
    ORDER BY CountryCode
    </cfquery>
    <cfdump var="#qCountries#">
    <!--- ========== END OF CODE ========== --->
    So running this returned the following error:
    Query Of Queries syntax error.
    Encountered "(. Incorrect Select Statement, Expecting a
    'FROM', but encountered '(' instead, A select statement should have
    a 'FROM' construct.
    Does anybody know why this doesn't work? Is it just not
    supported? Please note that I have also tried to use the CASE()
    function instead of DECODE() and that resulted in basically the
    same error. For now I an looping over my distinct query with a
    switch statement and manually loading a new query with the data how
    I want it. But it would be a lot cleaner and less code to have the
    DECODE() to work. Thx!

    DECODE() is an Oracle function, not generic SQL. Q-of-Q is a
    very limited subset of SQL and lacks many functions and clauses
    available in standard SQL, especially what you may be used to using
    in your particular RDBMS.
    See
    Query
    of Queries user guide
    Phil

Maybe you are looking for