Try this:
DECLARE @t DATETIME='06:51:00';
SELECT CAST(@t AS FLOAT)
The result
0,285416666666667
Your ridiculous value seems to be the part after the comma, meaning the fraction of a day. You could say After 28.54 percent of a full day it is 06:51
EDIT
Thx to Martin Smith I have to correct my "DATETIME is a FLOAT".
A DATETIME
is stored as two 4-byte-integers combined to an 8-byte-structure actually. The first int is taken as a day's count, the second is a counter of ticks
since midnight. You might read this.
Another common representation (I think this is valid for VB(A), Access and related systems) is a datetime
as float
. The integer part is the day's count and the fraction is the day's fraction. SQL Server converts this implicitly:
SELECT CAST(0.5 AS DATETIME)
The result is 1900-01-01 12:00:00.000
To get your time (the magic formula) you have to do this:
SELECT CAST(0.285571 AS DATETIME)
UPDATE the magic solution
DECLARE @t INT=285571;
SELECT CAST(CAST('0.' + CAST(@t AS VARCHAR(100)) AS FLOAT) AS DATETIME)
You might want to add one more CAST( ... AS TIME)
(depending on your SQL Server's version)
And for the fans of pure calculations
SELECT CAST(CAST(CAST(@t AS FLOAT)/CAST(POWER(10,CAST(LOG10(@t) AS INT)+1) AS FLOAT) AS DATETIME) AS TIME)
UPDATE
In your case it might be best to combine your day-counter and your time-value with a .
on string-level, cast this to float
and the result to datetime
.