I’m working with a db (SQL server 2008), and have an interesting issue with times stored in the db.
The DBA who originally set it up was crafty and stored scheduled times as smallints in 12-hour form– 6:00AM would be represented as 600. I’ve figured out how to split them into hours and minutes like thus:
select floor(time/100) as hr, right(time, 2) as min from table;
What I want to do is compare these scheduled times to actual times, which are stored in the proper datetime format. Ideally, I would do this with two datetime fields and use datediff() between them, but this would require converting the smallint time into a datetime, which I can’t figure out.
Does anyone have suggestions on how to do this?
Thanks in advance.
Can think of two ways to do that. The first is to build a string in the
HH:MMformat, and cast that todatetime. The second is to convert thesmallintformat tofloatwith the number of days as unit. The number of days is the internal time representation, so you can cast that todatetimetoo.Example code:
P.S. If you divide an integer by another integer. the result is a third integer:
100 / 24 = 4. If you divide an integer by a float, the result is a float:100 / 24.0 = 4.16666.