4 Replies Latest reply: Feb 11, 2011 8:52 PM by Hans Forbrich RSS

    Oracle 10g - Varchar(2) to Date conversion

    729133
      I have a single table, SOURCE_DATA,containing import data. The import data contains a date in the yyyymmddmmss(20110125032841) format and is stored
      in a varchar(2) field type named EVENT_DATE_AD when it is imported. When the import is complete, the EVENT_DATE_AD must be converted to a date(including time) and used to update the EVENT_DATE date type field. The table contains hundreds of thousands of records. What is the easiest way to import this data into the table, convert the date and update the EVENT_DATE field and set the CONVERT_DATE_CD = 1. The CONVERT_DATE_CD is a flag to verify this record has been processed. Right now I convert the .csv file to .xsl and import the data in to Oracle. I'm really not too concerned about the import process. Just the conversion and update. Any suggestions are appreciated.
        CREATE TABLE "SOURCE_DATA_TEST" 
         (     "CUID" NUMBER(9,0), 
           "EMAIL" VARCHAR2(255 BYTE), 
           "EVENT_DATE" DATE, 
           "EVENT_DATE_AD" VARCHAR2(20 BYTE), 
           "CONVERT_DATE_CD" NUMBER(1,0)
         ) ;
         
      INSERT INTO SOURCE_DATA_TEST VALUES (11133,'myemail.com',null,'20110125032841',0);  
      INSERT INTO SOURCE_DATA_TEST VALUES (11134,'myemail.com',null,'20110125032843',0);  
       
      commit;
        • 1. Re: Oracle 10g - Varchar(2) to Date conversion
          Etbin
          use external tables Re: External tables ?

          Regards

          Etbin
          • 2. Re: Oracle 10g - Varchar(2) to Date conversion
            Solomon Yakobson
            INSERT INTO SOURCE_DATA_TEST VALUES (11133,'myemail.com',null,TO_DATE('20110125032841','YYYYMMDDHH24MISS'),0);
            SY.
            • 3. Re: Oracle 10g - Varchar(2) to Date conversion
              729133
              This works for the date but the time is not stored. When I select on the EVENT_DATE the following displays:
              25-JAN-11
              I changed the NLS_DATE_FORMAT = MM/DD/RRRR.
              Now it displays
              2/25/2011.
              But, still no time. Any suggestions?
              • 4. Re: Oracle 10g - Varchar(2) to Date conversion
                Hans Forbrich
                achtung wrote:
                This works for the date but the time is not stored. When I select on the EVENT_DATE the following displays:
                25-JAN-11
                I changed the NLS_DATE_FORMAT = MM/DD/RRRR.
                Now it displays
                2/25/2011.
                But, still no time. Any suggestions?
                A column of type DATE will ALWAYS store the time component.

                However, most Oracle databases are configured to only display the date component when selecting a DATE column.

                To display the time component, you MUST ensure that it will be displayed. There are 3 ways to do that
                1) Configure the database to display the time component for all dates;
                2) Configure the SESSION to display the time component for all dates; and/or
                3) Use the TO_CHAR function and add the format to that displays the time component.

                Your NLS_DATE_FORMAT only is designed to show the date component. Use 'hhmiss' to add Hours, Minutes and Seconds.

                And read the fine SQL Langiuage Reference manual, chapter 2, to get an idea how to use the other several dozen formatting capabilities.