简体   繁体   English

如何在Excel中将Oracle内部日期转换为Excel日期?

[英]How do I convert Oracle internal date to Excel date in Excel?

I have a .csv file which was captured from an Oracle database. 我有一个从Oracle数据库中捕获的.csv文件。 The date in the .csv file is in the Oracle internal format (not sure what it's called as I'm not familiar with Oracle). .csv文件中的日期采用Oracle内部格式(不确定它的名称,因为我不熟悉Oracle)。 It looks like 16474 它看起来像16474

When I look at that date in the app we use to access the DB, the date is 2/6/2013 . 当我在应用程序中查看该日期时,我们用来访问数据库,日期是2/6/2013月6日。

However, when I import the .csv file into Excel and format the column as Short Date, it shows the date as 2/6/1945 但是,当我将.csv文件导入Excel并将列格式化为短日期时,它将日期显示为2/6/1945

I did a random sampling, and it looks like the month/day is always correct, but the year is off by 68 years. 我做了一个随机抽样,看起来月/日总是正确的,但年份是68年。 I'm assuming this is because Oracle's "beginning of time" is different than Excel's "beginning of time" 我假设这是因为Oracle的“开始时间”与Excel的“开始时间”不同

Any way to fix this so the dates show properly when importing the Oracle data into Excel? 有什么方法可以解决这个问题,以便在将Oracle数据导入Excel时正确显示日期?

I don't have access to anything on the Oracle side .. just the .csv file, and Excel. 我无法访问Oracle方面的任何内容..只有.csv文件和Excel。 I'm also ultimately importing the Excel file into Access if that helps. 如果有帮助的话,我最终还是将Excel文件导入Access。

So far, I formatted the Oracle date as Short Date in Excel, and inserted a column next to my date column (J) and used the following: 到目前为止,我在Oracle中将Oracle日期格式化为短日期,并在我的日期列(J)旁边插入一列,并使用以下内容:

=DATE(YEAR(J1)+68,MONTH(J1),DAY(J1))

I'm not sure if that is 100% accurate considering leap years 考虑到闰年,我不确定这是否100%准确

EDIT: I think this code may do it: 编辑:我认为这段代码可能会这样做:

Public Sub ReplaceData()

    Dim RangeCell As Range

    For Each RangeCell In Selection

        If IsNumeric(RangeCell.Value) Then
            lngDate = CLng(RangeCell.Value)
            RangeCell.Value = DateAdd("d", lngDate - 1, "1/1/1968")
        End If

    Next RangeCell

End Sub

It seems that Oracle may begin on 1/1/1968. 似乎Oracle可能会在1968年1月1日开始。 I'm not a DB guy but I think that number sounds familiar in other reading I have done. 我不是一个数据库人,但我认为这个数字在我所做的其他阅读中听起来很熟悉。

Here is an example routine that would convert the Oracle date to Excel date, if the above assumption of 1/1/1968 is true. 这是一个将Oracle日期转换为Excel日期的示例例程,如果上述1/1/1968的假设为真。

Sub OracleToExcelCSVDates()
Dim rng as Range 'range of cells containing dates'
Dim cl as Range
Dim strDate As String 'date received from Oracle'
Dim lngDate As Long
Dim newDate As Date 'Converted date'

Set rng = Range("J1:J1000") '< modify this for the number of rows you use in col J'

For each cl in rng 'Iterate over the rng you defined above.'
    'Capture the date from Oracle'
    strDate = cl.Value
    If Not Trim(strDate) = vbNullString Then 'ignore blanks'
        'convert to Long data type'
        lngDate = CLng(strDate)

        'Add the # of days (lngDate) to Oracles base date of 1/1/1968'
        newDate = DateAdd("d", lngDate - 1, "1/1/1968")

        'Overwrite the cell value with the new converted date:'
        cl.Value = newDate
    End If

End Sub

More info about Excel dates: 有关Excel日期的更多信息:

Excel stores serial dates as "a number representing the number of days since 1900-Jan-0, plus a fractional portion of a 24 hour day: ddddd.tttttt . This is called a serial date, or serial date-time." Excel将序列日期存储为“表示自1900年1月1日以来的天数的数字,加上24小时日的小数部分:ddddd.tttttt。这称为序列日期或序列日期时间。”

http://www.cpearson.com/excel/datetime . http://www.cpearson.com/excel/datetime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM