Change Excel date number to Oracle date

后端 未结 3 1914
滥情空心
滥情空心 2020-12-10 20:12

I\'m having date as 41293 in oracle, how can i show it in DD/MON/YYYY format?

If i copy pasted it in Excel and change it to date format, i

相关标签:
3条回答
  • 2020-12-10 20:41

    Microsoft's Documentation

    Excel stores dates as sequential serial numbers so that they can be used in calculations. January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900.

    Excel has a bug feature where it considers 1900 to be a leap year and day 60 is 1900-02-29 but that day never existed and a correction needs to be applied for this erroneous day.

    It does also state that:

    Microsoft Excel correctly handles all other leap years, including century years that are not leap years (for example, 2100). Only the year 1900 is incorrectly handled.

    Therefore only a single correction is required.

    So:

    • Before 1900-03-01 you can use DATE '1899-12-31' + value.
    • On or after 1900-03-01 you can use DATE '1899-12-30' + value.

    Which can be put into a CASE statement:

    SELECT CASE
           WHEN value >=  1 AND value < 60
           THEN DATE '1899-12-31' + value
           WHEN value >= 60 AND value < 61
           THEN NULL
           WHEN value >= 61
           THEN DATE '1899-12-30' + value
           END AS converted_date
    FROM   your_table
    
    0 讨论(0)
  • 2020-12-10 20:49

    The value you have is the number of days since the 30th of December 1899. Try:

    select to_char(
      to_date('1899-12-30', 'YYYY-MM-DD') + 41293,
      'DD/MON/YYYY') from dual
    
    0 讨论(0)
  • 2020-12-10 20:58

    Quoting from Oracle forum:

    You need a tool to do that, since format is to tell oracle what type of format you have on your date type in the spreadsheet. While you may not have opted to format the date in Excel, it will appear as a date in the previewer. Use the format from this as a guide to enter into the datatype panel.

    so, if you have a date that looks like this in the previewer, 19-jan-2006, then your format for the data type panel if you choose to insert that column is going to be DD-MON-YYYY,

    Option 1:

    Try using the below functions

    FUNCTION FROMEXCELDATETIME ( ACELLVALUE IN VARCHAR2 )
        RETURN TIMESTAMP
    IS
        EXCEL_BASE_DATE_TIME CONSTANT TIMESTAMP
                := TO_TIMESTAMP ( '12/31/1899',
                               'mm/dd/yyyy' ) ;
    
        VAL CONSTANT NUMBER
                := TO_NUMBER ( NULLIF ( TRIM ( ACELLVALUE ),
                                    '0' ) ) ;
    BEGIN
        RETURN EXCEL_BASE_DATE_TIME
              + NUMTODSINTERVAL ( VAL
                              - CASE
                                    WHEN VAL >= 60
                                    THEN
                                        1
                                    ELSE
                                        0
                                END,
                              'DAY' );
    END;
    
    FUNCTION TOEXCELDATETIME ( ATIMESTAMP IN TIMESTAMP )
        RETURN VARCHAR2
    IS
        EXCEL_BASE_DATE_TIME CONSTANT TIMESTAMP
                := TO_TIMESTAMP ( '12/31/1899',
                               'mm/dd/yyyy' ) ;
    
        DIF CONSTANT INTERVAL DAY ( 9 ) TO SECOND ( 9 )
                := ATIMESTAMP
                   - EXCEL_BASE_DATE_TIME ;
        DAYS CONSTANT INTEGER := EXTRACT ( DAY FROM DIF );
    BEGIN
        RETURN CASE
                  WHEN DIF IS NULL
                  THEN
                      ''
                  ELSE
                      TO_CHAR (  DAYS
                             + CASE
                                   WHEN DAYS >= 60
                                   THEN
                                       1
                                   ELSE
                                       0
                               END
                             + ROUND ( ( EXTRACT ( HOUR FROM DIF )
                                      + ( EXTRACT ( MINUTE FROM DIF )
                                        + EXTRACT ( SECOND FROM DIF )
                                          / 60 )
                                        / 60 )
                                     / 24,
                                     4 ) )
              END;
    END;
    

    Option 2:

    The excel function would be =TEXT(B2,"MM/DD/YY"), to convert an Excel date value stored in B2. Then try using the test character in Oracle

    If considering 1900 Jan 1st as start date,

    SELECT
          TO_CHAR ( TO_DATE ( '1900-01-01',
                          'YYYY-MM-DD' )
                  + 41293,
                  'DD/MON/YYYY' )
    FROM
          DUAL
    
    0 讨论(0)
提交回复
热议问题