DateTime saved as numeric value using SqlBulkCopy

asp.net c# interop sqlbulkcopy

Question

I'm using SqlBulkCopy to save a read-only Excel sheet into a predefined SQL table.
There are several date columns in the sheet, but not all the rows have date values(empty and '#').
The corresponding columns types are varchar(20) and not smalldatetime, because otherwise I get an error that the data can not be converted.

After running SqlBulkCopy the dates are saved as numeric values in a pattern that I can't figure out.
For example:
31.12.2010 is saved as 40543
31.13.2011 is saved as 40908
30.6.2011 is saved as 40724

Now I have to present the date in web forms. The numeric values don't bother me as long as I can convert them back to date format for presenting in a GridView, but I don't know how.

Would appreciate any help with this.

Accepted Answer

I used the following:

DateTime.FromOADate(num).ToShortDateString();

where num is the numeric value of the date


Popular Answer

Try running this query:

SELECT CAST(CAST(YourVarcharDateColumn AS int) AS DateTime) FROM YourTable


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why