views:

72

answers:

1

My application stores dates as OLE Automation doubles with the DateTime.ToOADate() command.

Now, I need to create a SQL view wich shows me the Date stored. How can I quickly convert the double to a date?

Thanks

+3  A: 

Does

SELECT CAST(CASE WHEN OLEFLOAT > 0 THEN 
                         OLEFLOAT-2.0 
                 ELSE 
       2*CAST(OLEFLOAT AS INT) - 2.0 +  ABS(OLEFLOAT) END as datetime)

work? From here

An OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.

That sounds pretty much like the same system SQL Server uses when you cast a date as a float except the offset needed to be fiddled by 2 and for "negative" dates. SQL server will substract backwards. So -1.25 is 18:00 whereas for OLE that means 06:00.

Martin Smith
yeah, I was dumb and first asked before tryed the cast or convert option.... But now I have another problem. In MS SQL 2005/8 the value 1 represents 2 January of 1900, so all my dates are plus 2 days! Is this normal?
Nuno Agapito
@Nuno I found that as well. I've adjusted my post. There's another slight wrinkle as well with negative dates. I'll update again in a few minutes.
Martin Smith