0
votes

I am working on an application that deals with loading some SAS datasets into SQL using C#. I use the SAS OLE DB Adapter to load the SAS dataset into C# data table and its working. However, the datevalues which is stored as string datatype in the SAS file is getting loaded as 10 digit number and stored as string format in the data table.

Hence, I tried to convert them to datetime however I am getting only the date value and time value is 00:00:00 for all the records. The source has the date and time values for each record. Below is my code that I use to convert the string to datetime string.

public string ComputeDate(string sSourceData)
{
    string sProcessedDate = string.Empty;
    int iDateValue = ((Convert.ToInt32(sSourceData) / 86400) + 21916);
    Double dDate = Convert.ToInt32(iDateValue);
    DateTime dtDateValue = DateTime.FromOADate(dDate);
    sProcessedDate = dtDateValue.ToString();

    return sProcessedDate;
}

The source data sample and the data that is getting loaded in the data table is below

Source and DataTable

The output after converting the OADate is below

Final Output

It would be great if someone can help me out in providing me an solution for getting the output along with original time value provided in the source.

Thanks in advance.

2

2 Answers

1
votes

Integer ratios such as

Convert.ToInt32(sSourceData) / 86400

will yield an integer.

Ensure the result contains the day fraction ([0..1]) which is the time part in SQL / C#

Double dDate = Convert.ToInt32(sSourceData) / 86400.0 + 21916.0;
0
votes

Found the solution for my problem.

Instead of setting the datatype as Int, I need to set the datatype as double so that the timestamp of the OADate will be coming as decmimal values.

This way its getting output as datetime.

public string ComputeDate(string sSourceData)
{
    string sProcessedDate = string.Empty;
    Double iDateValue = ((Convert.ToDouble(sSourceData) / 86400) + 21916);
    DateTime dtDateValue = DateTime.FromOADate(iDateValue);
    sProcessedDate = dtDateValue.ToString();

    return sProcessedDate;
}