Using DECODE Function to change data

I am trying to use the Decode function in a SQL statement to find a field that has a specific type, and when it finds that type, I want to blank out the results in a different field.
For example:
DECODE(ADDR_TYPE,'HOME',PHONE='') HOME_PHONE

something like:
SQL> with t as
  2   (select 219 id,
  3           'BUS' addr_type,
  4           '505-555-5555' phone
  5      from dual
  6    union
  7    select 219 id,
  8           'HOME' addr_type,
  9           null   phone
10      from dual
11    union
12    select 220 id,
13           'BUS' addr_type,
14           '101-111-1111'   phone
15      from dual
16    union
17    select 220 id,
18           'HOME' addr_type,
19           null   phone
20      from dual
21    union
22    select 223 id,
23           'BUS' addr_type,
24           '202-222-2222'   phone
25      from dual
26    union
27    select 224 id,
28           'HOME' addr_type,
29           '303-333-3333'   phone
30      from dual
31    union
32    select 225 id,
33           'BUS' addr_type,
34           null   phone
35      from dual
36    union
37    select 226 id,
38           'HOME' addr_type,
39           null   phone
40      from dual)
41  select a.id,
42         a.addr_type,
43         decode(a.addr_type,'BUS',phone,null) phone
44    from (select id, addr_type, phone,
45                 row_number() over (partition by id order by id, decode(addr_type,'BUS',1,2)) rn
46            from t) a
47   where a.rn = 1;
        ID ADDR PHONE
       219 BUS  505-555-5555
       220 BUS  101-111-1111
       223 BUS  202-222-2222
       224 HOME
       225 BUS
       226 HOME
6 rows selected.
SQL>

Similar Messages

  • 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

  • How to use decode function

    Hello Friends,
    I have a query that has different columns and I am not sure what the data type of each column is ...
    I want to use decode function which displays the following if the value of the column is like this ..
    if the value of column is -714E ie -700000000000000 then display as .A
    if the value of column is -814E ie -800000000000000 then display as .B
    NOTE ; don't know which column is having the value - and i don't know the data type of the column is ..
    got to use the decode function for all the columns selected ..
    I want to use decode function pls let me know how to write for this kind of requirement.
    appreciate your help in this rgds.
    thanks/kumar

    Dear Sir / Madam,
    Thanks for your understanding.
    Right now I am facing a unique problem.
    I am using decode function in a select statement. The columns are dynamic some columns are of type number and some are varchar datatypes. I want to apply decode function for every column irrespective of datatype.
    What i am finding difficult is if the column is a number datatype , the decode funciton is working good but if the column datatype is varchar or char datatype then i am getting ..
    here's the decode function..
    decode (CHAI.EVNDRIVE,
    -800000000000000,'.A',
    -700000000000000, '.B',
    -600000000000000 ,'.C',
    -500000000000000 , '.D',
    -400000000000000 , '.E',
    -300000000000000 , '.F',
    -200000000000000, '.G',
    -100000000000000 , '.H',
    -1000000000, '.R' ,CHAI.EVNDRIVE ) EVNDRIVE
    report error:
    ORA-01722: invalid number
    pls let me know how to over come this kind of scenario.
    is their a way to find whether the column datatype is number or char , so that i can check the column datatype before the decode funciton is applied ..
    thanks ..
    kumar

  • Is sql server using trigger to capture change data in SQL server CDC?

    hi all,
    what is the Architecture of SQL server change data capture(CDC)? is sql server using trigger to capture change data in SQL server CDC? for example Change Data Capture (CDC) in SQL Server 2008 using we can capture change Data records INSERTs, UPDATEs, and
    DELETEs applied to SQL Server tables.so my question is SQL server using triggers to capture change data like INSERT TRIGGER, UPDATE TRIGGER and DELETED TRIGGER using each tables after enabled CDC...?
    Thanks
    Tharindu Dhaneenja 
    Tharindu Dhaneenja http://dhaneenja.blogspot.com

    It is not using triggers. The source is the transaction log.
    Related link:
    Basics of Change Data Capture
    ...The source of change data for change data capture is the SQL Server transaction log. As inserts, updates, and deletes are applied to tracked source tables, entries that describe those
    changes are added to the log. The log serves as input to the change data capture capture process. This reads the log and adds information about changes to the tracked table’s associated change table. Functions are provided to enumerate
    the changes that appear in the change tables over a specified range, returning the information in the form of a filtered result set. The filtered result set is typically used by an application process to update a representation of the source in some external
    environment. ...
    Kalman Toth, SQL Server & Business Intelligence Training; SQL Server 2008 Training

  • How to use DECODE function in Exspression?

    Hi,
    Can we use DECODE in Expression?
    I'm trying to use DECODE function but there is an error during the validation. But when i validate the mapping, it is successfully compiled but it is failed during deployment.
    But if I use CASE instead of DECODE, it works fine.
    Can we use DECODE in OWB???
    Thanks
    Raj

    Hi,
    In OWB 10gR2, if your are using only one DECODE in an expression, it's working. The package will compile when deploying the mapping. OWB will replace the DECODE by a CASE.
    But when you are using nested decode in an expression ( for example : decode(col1, 1, 'M', decode(col2, 'Madame', 'Mme', null)) ) only the first one is replaced by a case at deployment.
    In ROW_BASED mode, text of the expression is used outside of an sql statement and deployment will fails with "PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only."
    If operating mode for the mapping is set to SET_BASED, it's working because the expression is used in an sql statement.
    I have logged a SR in metalink for this issue and a bug is opened (bug 5414112).
    But I agree with you, it's better to use case statement.
    Bernard

  • How can we use DECODE function in where clause.

    Hi Guys,
    I have to use DECODE function in where clause.
    like below
    select * from tab1,tab2
    where a.tab1 = b.tab2
    and decode(code, 'a','approved')
    in this manner its not accepting?
    Can any one help me on this or any other aproach?
    Thanks
    -LKR

    >
    I am looking for to decode the actual db value something in different for my report.
    like if A then Accepted
    elseif R then Rejected
    elseif D then Denied
    these conditions I have to check in where clause.
    >
    what are you trying to do?
    may be you are looking for
    select * from tab1,tab2
    where a.tab1 = b.tab2
    and
       (decode(:code, 'A','Accepted') = <table_column>
        or
        decode(:code, 'R','Rejected') = <table_column>
       or
        decode(:code, 'D','Denied') = <table_column>
       )

  • Table for Function modules - Changed date & Changed by.

    Hi Experts,
    Help me to get Function module changed date & changed by.
    For programs we can get the mentained information in TABLE : TADIR.

    Function Modules change history can be retrieved from table TRDIR.
    Function Module stores a program Include in table TRDIR.
    The details of the Function Module can be found in Attributes-->general data tab in the Function Module.
    You need to pass the Include which correponds the FM.
    Hence you can find the Log changes.
    Hope this helps.
    Regards
    Vinayak

  • DECODE function to validate date value and sort the records

    Hi Friends,
    I am looking for some query which can give me the required output,
    I need to do this using SQL query only and I have tried using the MIN() and MAX() functions it was working fine with limited data, now after inserting the last record(in the below table) which has date for start_range and end_range in(mm/dd/yyyy hh24:mi:ss) format.
    Because the data type is VARCHAR2 if I am using the MIN() function it is sorting as a string value, hence the min date is incorrect. I tried using validating the value to date or non date and tried to using MIN() and MAX() functions using the DECODE function, I am getting this error "ORA-01840: input value not long enough for date format".
    select table_name,
    DECODE(substr(START_RANGE,3,1)||substr(START_RANGE,6,1)||substr(START_RANGE,11,1)||substr(START_RANGE,14,1)||substr(START_RANGE,17,1),'// ::',
    to_char(min(to_date(start_range,'mm/dd/yyyy hh24:mi:ss')),'MM/DD/YYYY HH24:MI:SS'),min(start_range)) MIN_RUNS_START_RANGE,
    DECODE(substr(END_RANGE,3,1)||substr(A.END_RANGE,6,1)||substr(END_RANGE,11,1)||substr(END_RANGE,14,1)||substr(END_RANGE,17,1),'// ::',
    to_char(max(to_date(END_RANGE,'mm/dd/yyyy hh24:mi:ss')),'MM/DD/YYYY HH24:MI:SS'),max(END_RANGE)) MAX_RUNS_END_RANGE
    from MY_TABLE
    GROUP BY table_name,
    (substr(START_RANGE,3,1)||substr(START_RANGE,6,1)||substr(START_RANGE,11,1)||substr(START_RANGE,14,1)||substr(START_RANGE,17,1)),
    (substr(END_RANGE,3,1)||substr(END_RANGE,6,1)||substr(END_RANGE,11,1)||substr(END_RANGE,14,1)||substr(END_RANGE,17,1))
    Can sombody please advise what is the best way I can query this data with the required output.
    The following are the source table and records and the out put records using the sql query.
    MY_TABLE
    TABLE_NAME(VARCHAR2),START_RANGE(VARCHAR2),END_RANGE(VARCHAR2)
    TABLE1,1000,10000
    TABLE2,ABCD,EEEE
    TABLE3,01/12/2010 00:00:00,12/31/2010 23:59:59
    TABLE1,10001,20000
    TABLE2,EEEF,GGGG
    TABLE3,01/01/2011 00:00:00,01/31/2011 23:59:59
    OUTPUT :
    TABLE_NAME,MIN(START_RANGE),MAX(END_RANGE)
    TABLE1,1000,20000
    TABLE2,ABCD,GGGG
    TABLE3,01/12/2010 00:00:00,01/31/2011 23:59:59
    Thanks
    Kalycs

    i also think this is a very bad table design ...
    but if you are not able to change it, you could split the select (date and non-date data) and combine the result with UNION like this:
    with t as
    SELECT 'TABLE1' table_name,'1000' start_range,'10000' end_range FROM dual UNION
    SELECT 'TABLE2','ABCD','EEEE' FROM dual UNION
    SELECT 'TABLE3','01/12/2010 00:00:00','12/31/2010 23:59:59' FROM dual UNION
    SELECT 'TABLE1','10001','20000' FROM dual UNION
    SELECT 'TABLE2','EEEF','GGGG' FROM dual UNION
    SELECT 'TABLE3','01/01/2011 00:00:00','01/31/2011 23:59:59' FROM dual
    SELECT table_name,
           TO_CHAR(MIN(TO_DATE(start_range, 'MM/DD/YYYY HH24:MI:SS')), 'MM/DD/YYYY HH24:MI:SS'),
           TO_CHAR(MAX(TO_DATE(end_range, 'MM/DD/YYYY HH24:MI:SS')), 'MM/DD/YYYY HH24:MI:SS')
    FROM t
    WHERE start_range LIKE '%/%/%:%:%'
    GROUP BY table_name
    UNION
    SELECT table_name,
           MIN(start_range),
           MAX(end_range)
    FROM t
    WHERE start_range NOT LIKE '%/%/%:%:%'
    GROUP BY
        table_name;
    TABLE_ MIN_VALUE           MAX_VALUE
    TABLE1 1000                20000
    TABLE2 ABCD                GGGG
    TABLE3 01/12/2010 00:00:00 01/31/2011 23:59:59

  • 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

  • How to use Decode Function in Webi / Designer - BOE XI 3.1

    Hi All,
    I have a SQL query which needs to include in the webi report.
    Below is the query :
    SELECT
    SECURITY.SEC_CUSIP_NO "CUSIP", 
    SECURITY.STY_SEC_TY_CD "SECURITY TYPE",
    SECURITY.SEC_DERIVED_DESC_TX "SECURITY DESCRIPTION",
      SECURITY.sec_dep_teleg_de "FED DESCRIPTION",
      SEC_STND_PR "STANDARD/FACTORED PRICE",
      SEC_STND_PR_EFF_DT "STANDARD/FACTORED PRICE Date",
      SECURITY.SEC_YIELD_PR "YIELD PRICE",
      SECURITY.SEC_YIELD_PR_DT "YIELD RICE EFFECTIVE DATE",
      STND_PR_VND_VENDOR_CD "PRICE SOURCE",
      SEC_MATURITY_DT "MATURITY DATE",
      SEC_ISSUE_DT "ISSUE DATE",
                 CASE WHEN SECURITY.STY_SEC_TY_CD IN ('BA','CD','CDD','CDE','CDM','CDV','CP','CPD') THEN
                    (position.PSN_AVAIL_PAR_VL + position.PSN_COLLAT_PAR_VL) * (SECURITY.SEC_STND_PR) / 100
                 ELSE
                    (position.PSN_AVAIL_PAR_VL + position.PSN_COLLAT_PAR_VL) * (  (  SECURITY.SEC_YIELD_PR * DECODE (SECURITY.SEC_PRIN_FT, 0, 1, SECURITY.SEC_PRIN_FT))) / 100
                    * DECODE(SECURITY.STY_SEC_TY_CD, 'AMP', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 'MMP', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 'AMPT', DECODE(NVL(SECURITY.SEC_MULTIPLIER_UNIT_VL,0), 0, 1, SECURITY.SEC_MULTIPLIER_UNIT_VL), 1 )
                 END
            ) "MARKET VALUE"
    FROM SECURITY SECURITY
       ,   position position
    WHERE STND_PR_VND_VENDOR_CD = 'VT'
    AND SEC_MATURITY_DT > SYSDATE-1
    and SECURITY.sec_sys_id=position.sec_sys_id
    I could create a report which has selected objects and defined where condition from the above query. I have also created a variable for 'when' & 'then' condition but stuck at else part which contains 'DECODE' function.
    Please suggestions how to write the same at the report or designer level.
    Thanks,
    Rameez Shaikh

    Hi Rameez,
    Looking at the query you can create the object directly in the universe, either create two objects one for inner decode and use it in outer case logic. In report it is nothing but nested if logic. For eg.  If(a=b;1;(if(a=c;2;3))
    Thanks
    Gaurav

  • Using DECODE Function in SQL

    I need to know if there is any way to use a range of values from
    database and decode to certain text. I am able to do with one
    value.
    for example:
    DECODE(column_name,'216767111','Unlimited',column_name)
    above argument works with one value only. How about a range,
    ex: 216767000 to 216767111. I need to use only SQL. No PL/SQL.
    Kinldly need some body's help
    Thanks
    Munis

    Which version of the database? If it's 8i+ then you can use
    the CASE function
    for example:
    (CASE WHEN column_name between 216767000 and 216767111
    THEN 'Unlimited' ELSE column_name END)
    This won't work in PL/SQL because they're introducing a CASE
    statement does soemthing different.
    rgds, APCHello Andrew
    Thank you for response. I am using 8i. 8.1.6. However using
    CASE, I get inconsistent data type, ORA-00932: inconsistent
    datatypes. I able to work it out with other response using
    DECODE(sign(. Do you have any idea why i am getting this error.
    If time permits, let me know

  • How to use decode function in oracle apex 3.2.1

    Hello Friends,
    how to use decode
    var_decode := 'decode'||'('|| var1 ||','|| -800000000000000||','||'.A'||','||
    appreciate your help.
    regards/kumar
    Edited by: kumar73 on Apr 23, 2010 12:25 PM
    Edited by: kumar73 on Apr 23, 2010 12:27 PM

    Hey just on this particular point, there's a handy PL/SQL method for escaping quotation characters which may reduce the complexity of the string.
    For example, if I had a string:
    I can't understand why quoting my 'quote' characters never seems to "work"!and I wanted to pass that into a variable, I could do this:
    vc_foo:= 'I can''t understand why my ''quote'' characters never seems to "work"!';or I could use the q function, whereby you enclose your string within a quote delimiter of your choice, like so:
    vc_foo:= q'^I can't understand why quoting my 'quote' characters never seems to "work"!^';
    note: I used the caret (^) character by convention but any valid character will do
    ...all of which leaves your original string more or less unadulterated.
    I find this particularly useful for generating dynamic SQL and dynamic PL/SQL, as it leave the query looking a tad more readable, although if you are chaining together multiple strings with double-pipes, it can make it a little muddier. I find it particularly useful if I'm going to deal with string data from, for example, a field with Irish-derived surnames such as "O'Reilly"...
    Give it a go!

  • Planning function to change date field value in layout

    Hello experts,
                          I have 1 BPS layout with 2 date fields ( Start date, End date ). With a filter conditions, set of data comes in BPS layout on execution and 2 date fields also contain some value...I want to change values of these 2 dates...both the date fields are char infoobjects..in BPS layout, in planning function I got only 1 function ( Repost function ) to change the value of char infoobject based on some condition. but date field value is not the status field...i want to enter new date instead of old date..if i right exit function in that also i can change only keyfigs values..so please help me to solve this problem..
    Thanks & Regards,
    Priyanka Joshi

    Hi Priyanka,
    As of now master data planning is not so flexibly supported.
    Still u can refer the below link which is enhancement of standard Repost function,and can be used to change master data.
    [http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/10d2b273-0e12-2c10-fab3-a34bde559f92]
    As far as my knowledge goes u cannot directly input date in rows.
    regards,
    Rajendra

  • No longer able to use + and - keys to change dates or time?

    I recently upgraded from an igloo iMac running OS 10.3.9 to a new Mac mini with 10.5.8 (awaiting upgrade to Snow Leopard).
    I used to use the + and - keys all the time to change dates and time in iCal, but have been unable to since my upgrade. Has that functionality been deleted, or is there a way to do it that I have just not discovered yet?
    Thanks.

    Forgotten Passcode or device disabled after entering wrong passcode
    If you have Find My iPhone enabled, you can use Remote Wipe to erase the contents of your device. If you have been using iCloud to back up, you may be able to restore the most recent backup to reset the passcode after the device has been erased.
    Alternatively, place the device in recovery mode and restore it to erase the device:
    Disconnect the USB cable from the device, but leave the other end of the cable connected to your computer's USB port.
    Turn off the device: Press and hold the Sleep/Wake button for a few seconds until the red slider appears, then slide the slider. Wait for the device to shut down.
    While pressing and holding the Home button, reconnect the USB cable to the device. The device should turn on.
    Continue holding the Home button until you see the Connect to iTunes screen.
    iTunes will alert you that it has detected a device in recovery mode. Click OK, and then restore the device.

  • Using DECODE() to insert to DATE field

    I'm trying to use the DECODE function to test for NULL before inserting to a DATE field. However, it seems to only insert the DATE, with a "default" time of 12:00 - it isn't properly inserting the time.
    Basically I need to test if Date1 is NULL. If it isn't I need to concatenate the DATE from Date1 with the TIME from Date2 to get a full date/time... then insert this new value.
    Generic Example:
    CREATE TABLE DATETEST (TestID NUMBER(1), TestDate DATE);
    DECLARE
    v_Date1 DATE;
    v_Date2 DATE;
    BEGIN
    v_Date1 := TO_DATE('01-JAN-11 05:53:12', 'DD-MON-YY HH:MI:SS');
    v_Date2 := TO_DATE('08-FEB-11 02:18:31', 'DD-MON-YY HH:MI:SS');
    INSERT INTO DATETEST (TestID, TestDate) VALUES ('1', DECODE(v_Date1, NULL, NULL, TO_DATE(To_Char(v_Date1, 'DD-MON-YY') || ' ' || TO_CHAR(v_Date2, 'HH:MI:SS'),'DD-MON-YY HH:MI:SS')));
    INSERT INTO DATETEST (TestID, TestDate) VALUES ('2', TO_DATE(To_Char(v_Date1, 'DD-MON-YY') || ' ' || TO_CHAR(v_Date2, 'HH:MI:SS'),'DD-MON-YY HH:MI:SS'));
    END;
    SELECT TestID, TO_CHAR(TestDate, 'DD-MON-YY HH:MI:SS') from DATETEST;
    This example performs two inserts. One with the DECODE function, and one without. The one without inserts the time properly. Can anyone tell me why the one with the DECODE function doesn't? I realize I can use a simple if/then to check if the date is null above and put the date/time in a variable, but since my real scenario is in a large chunk of other stuff, I'm trying to keep it as streamlined as possible.
    Edited by: BoredBillJ on Jul 14, 2011 6:39 AM

    The problem you are having is due to the nature of how DECODE and CASE determine what datatype to return, and you nls_date_format settings. Both use the data type of the first returnable argument to determine all of them. So, in your decode statement, the first returnable value is NULL which, in the absence of a cast (either implicit or explicit), is a varchar2 column. So, if the date is not null, the implicit conversion to a varchar to match the retunr type, then back to date to insert into the table is losing the time. you need something more like:
       INSERT INTO test_date (Test_ID, TestDate)
       VALUES ('1', DECODE(v_Date1, NULL, TO_DATE(NULL),
                                          TO_DATE(To_Char(v_Date1, 'DD-MON-YY') || ' ' ||
                                          TO_CHAR(v_Date2, 'HH:MI:SS'),'DD-MON-YY HH:MI:SS')));Even if you use Solomon's method of generating the date, if you need the decode/case, then you will have to either use the TO_DATE(NULL) or use case instead of decode and reverse the test so the first returnable is a date like:
    SQL> DECLARE
      2     v_Date1 DATE;
      3     v_Date2 DATE;
      4  BEGIN
      5     v_Date1 := TO_DATE('01-JAN-11 05:53:12', 'DD-MON-YY HH:MI:SS');
      6     v_Date2 := TO_DATE('08-FEB-11 02:18:31', 'DD-MON-YY HH:MI:SS');
      7     INSERT INTO test_date (Test_ID, TestDate)
      8     VALUES ('1', CASE WHEN v_date1 IS NOT NULL
      9                       THEN TO_DATE(To_Char(v_Date1, 'DD-MON-YY') || ' ' ||
    10                                    TO_CHAR(v_Date2, 'HH:MI:SS'),'DD-MON-YY HH:MI:SS')
    11                       ELSE NULL END);
    12     INSERT INTO test_date (Test_ID, TestDate)
    13     VALUES ('2', TO_DATE(To_Char(v_Date1, 'DD-MON-YY') || ' ' ||
    14                  TO_CHAR(v_Date2, 'HH:MI:SS'),'DD-MON-YY HH:MI:SS'));
    15  END;
    16  /
    PL/SQL procedure successfully completed.
    SQL> select test_id, to_char(testdate, 'dd-mon-yyyy hh24:mi:ss')
      2  from test_date;
       TEST_ID TO_CHAR(TESTDATE,'DD
             1 01-jan-2011 02:18:31
             2 01-jan-2011 02:18:31John

Maybe you are looking for

  • Object reference not set message popup

    Dear Fellow Teststand Developers, I am developing tests using the C# adapter. From time to time I get the following message when running the tests: I've been unable to locate the error because it dousn't happen very often. The test keeps running when

  • Need to print sheet of multiple identical labels from one data record

    Post Author: mhamill CA Forum: General I need to print 18 labels on a sheet 3 columns x 6 rows of the SAME LABEL based on a single data record, I've got the label size all set but can only find a way to produce 1 label if I only have 1 record in the

  • Duplicate HTTP requests from Flex app in Safari 4.0.3

    We are currently trying to debug a problem a Mac user is having with our Flex based site, where almost every http operation he does is duplicated. e.g. "GET /path/file.xml HTTP/1.1" 200 3543 "https://www.site.com/index.jsp" "Mozilla/5.0 (Macintosh; U

  • Adding switch into an existing stack

                       Hi all,     I have an existing stack which comprises of 6 switches, please help with the following questions : 1/ adding another switch into this stack will cause all switches in stack to reboot ? anything that I should be aware of

  • Illegal text relocation when linking ANE

    Hi there, I'm getting this issue when trying to link my ANE with ffmpeg libs which I build on Mac OS X Snow Leopard (XCode 4.2) ld: illegal text relocation to _uyvyToY_c in ..../libswscale.a(input.o) from _ff_sws_init_input_funcs in ...../libswscale.