Summary: How to extract records that belong to the UTC time interval, when the UTC time in the key column is encoded?
The UTC encoding: I do have a third-party table where records contain UTC time encoded as strings like '2456065.80700942:0000002F'. To get the UTC of the DATETIME type, the following formula can be used:
CAST((CAST(LEFT(encodedUTC, 16) AS FLOAT) - 2415020.5) AS DATETIME)
They say, the part before the colon is the Julian date encoded to float (and the float to the string). I did not check, it seems to work.
The query: Now I have @fromUTC and @toUTC of the DATETIME type. I can select:
SELECT CAST((CAST(LEFT(d.UTC, 16) AS FLOAT) - 2415020.5) AS DATETIME) AS UTC,
...
FROM dbo.Data AS d
WHERE CAST((CAST(LEFT(d.UTC, 16) AS FLOAT) - 2415020.5) AS DATETIME) > @fromUTC
AND CAST((CAST(LEFT(d.UTC, 16) AS FLOAT) - 2415020.5) AS DATETIME) < @toUTC
(I am not using BETWEEN as I do not want to get the records with equal times.) It works, but the problem is that the conversion formula is repeated 3 times (and I do not like the repeating of the same code if it is not neccessary).
My question is: How can I write the SELECT better?
Thanks for your time and information,
Petr
I’d consider either adding a view that includes this calculation, or a computed column on the table itself (if you can change the table definition).
Either of these approaches would lead to a cleaner final select. There’s likely some performance considerations based on how often you select versus insert, so will probably shade you one way or the other.