How do I convert Oracle internal date to Excel date in Excel? - oracle

I have a .csv file which was captured from an Oracle database. 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). It looks like 16474
When I look at that date in the app we use to access the DB, the date is 2/6/2013.
However, when I import the .csv file into Excel and format the column as Short Date, it shows the date as 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. I'm assuming this is because Oracle's "beginning of time" is different than Excel's "beginning of time"
Any way to fix this so the dates show properly when importing the Oracle data into Excel?
I don't have access to anything on the Oracle side .. just the .csv file, and Excel. I'm also ultimately importing the Excel file into Access if that helps.
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:
=DATE(YEAR(J1)+68,MONTH(J1),DAY(J1))
I'm not sure if that is 100% accurate considering leap years
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. 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.
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 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."
http://www.cpearson.com/excel/datetime.

Related

PowerBI unable to detect time type in get data

I get data from a folder where my file name has format Watches_20220315_095127 its in date and time format. But Power BI is unable to detect when the time starts with "0" i.e 095127 it displays error. Can you please help me. I need that time from file name to be used in powerbi report. Power BI works fine when my time has the following format.
Watches_20220315_105127
Watches_20220307_184253
Watches_20220301_144421
PowerBI not detecting as time for below format
Watches_20220315_095127
Assuming that you have the file name as a column in Power Query (PQ), you need to convert that text into a format that PQ can turn into a Date. You can do something like this by getting the sections of the string using Text.Range (Where [Column1] is the name of your column).
DateTime.FromText(
Text.Range([Column1], 8, 4) & "-"
& Text.Range([Column1], 12, 2) & "-"
& Text.Range([Column1], 14, 2) & " "
& Text.Range([Column1], 17,2) & ":"
& Text.Range([Column1], 19,2) & ":"
& Text.Range([Column1], 21,2)
)
This will split out the text into a YYYY-MM-DD HH:MM:SS format that can use DateTime.FromText to convert it into a date time.
You will have to set the field to be a date time type for PQ and then DAX to pick it up as a date time. In the below example the 4th row that has a time of 09:30:01 will now be converted to a correct date time.

change year in date irrespective of date format using vbscript

I want to only change year in the date irrespective of date format. For example if date=05/20/2019 0r 05-20-2019 then it should change to 05/20/2017
Just use DateSerial() to replace the Year component.
Call SetLocale("en-us")
Dim oldDate: oldDate = CDate("05/20/2019")
Dim newDate: newDate = DateSerial(2015, Month(oldDate), Day(oldDate)

Change System Custom Data

I use to program in VB6 and started the Visual Studio VB thing for past 6 months or so. I need to find a way to change the users PC system date format to custom format using VS2010
Without asking the user to go through the control panel
I had a look at the following code but it does not permantly set the system date format
Dim en As New CultureInfo("en-US")
Thread.CurrentThread.CurrentCulture = en
' Creates a DateTime for the local time.
Dim dt As New DateTime(2001, 7, 13, 4, 0, 0)
' Converts the local DateTime to the UTC time.
Dim utcdt As DateTime = dt.ToUniversalTime()
' Defines a custom string format to display the DateTime.
' zzz specifies the full time zone offset.
Dim format As [String] = "MM/dd/yyyy hh:mm:sszzz"
' Converts the local time to a string
' using the custom format string and display.
Dim str As [String] = dt.ToString(format)

Excel for Mac VBA - Go to specific row of table and work up the rows to first row

I´d like to find a specific row of a table in Excel for Mac 2011. From this row, I want to work up the table to the 1st row. I´m sure there is a simple way of doing it.
Also, what is the tweak, to work down to the end of the table from a specified row?
Thanks in Advance!!
You might want to provide an idea of what you have tried...as well as how you want to find the starting row. Regardless, here I have finding the end row of your data (based on column A, change as needed). This goes to the bottom of the sheet, then essentially does Ctrl + up arrow to get the last row that has something in it. Then starts from there and steps backwards through the rows until row 1. Nb you don't have to set i to endRow, I just did in case you wanted to use endRow for something else.
Public Sub testing()
Dim ws As Worksheet
Dim endRow As Integer
Dim i As Integer
Set ws = ThisWorkbook.Worksheets("Sheet1")
endRow = ws.Range("A" & Rows.Count).End(xlUp).row
i = endRow
While i >= 1
'Working up the sheet doing whatever
i = i - 1
Wend
End Sub

How to select Monthly and yearly Dates?

Using VB 6
Using two DTPicker in my project, I want to select monthly and yearly like MM/yyyy and yyyy
Now am Using Daily Date’s like 23/02/2009 (dd/MM/yyyy), I want to use like Monthly (MM/yyyy) and Yearly (yyyy)
My Code
Public Function DaysInMonth(dte As Date) As Integer
Dim d As Date
d = DateAdd("m", 1, dte)
d = DateAdd("d", -1, d)
DaysInMonth = Day(d)
End Function
Private Sub Form_Load()
Dim dteFrom As Date
Dim dteTo As Date
dteFrom = Format(CDate(Year(Date) & "-" & Month(Date) & "-" & "01"), "yyyy-mm-dd")
dteTo = Format(CDate(Year(Date) & "-" & Month(Date) & "-" & DaysInMonth(dteFrom)), "yyyy-mm-dd")
dtpFrom.Value = dteFrom
dtpTo.Value = dteTo
End Sub
Above code is working for daily dates. I want to use monthly and yearly dates also
Now I am selecting a date it showing complete value of the date like 03-02-2009, I want to show monthly and Yearly data’s
Expected Output.
Monthly Date
03/2009 to 04/2009 – It should take a value of 01/03/2009 to 30/04/2009
05/2009 to 05/2009 – It should take a value of 01/05/2009 to 31/05/2009
Yearly Date
2008 to 2008 – It should take a value of 01/01/2008 to 31/12/2008
2008 to 2009 – It should take a value of 01/01/2008 to 31/12/2009
How to select monthly and yearly?
Need VB 6 code Help
You can set the Format on the DTPicker to 3 (dtpCustom) and the CustomFormat to MM/yyyy . This will display month/year format in the text section, but you will still get the full calendar on drop down.
If you want to disable the calendar, you can set UpDown to True. Then you will get up/down buttons instead of the dropdown.
You need to make sure that the hidden value (day part) is correct.
EDIT:
OK, from your comment I understand that you are happy with the date picker, but you want to display the result in a special format?
Example:
Date selected is 20/8/2009 and stored in a date variable a.
Display as 2009:
MsgBox Format(a, "yyyy")
Display as 08/2009:
MsgBox Format(a, "mm/yyyy")
Display as August, 2009:
MsgBox Format(a, "mmmm, yyyy")
Display as Aug, 2009:
MsgBox Format(a, "mmm, yyyy")
Is this what you want?

Resources