views:

42

answers:

3
+1  Q: 

Float as DateTime

SQL Server 2008

I almost have, I think, what I'm looking to do. I'm just trying to fine tune the result. I have a table that stores timestamps of all transactions that occur on the system. I'm writing a query to give an average transaction time. This is what I have so far:

With TransTime AS (
  select endtime-starttime AS Totaltime 
    from transactiontime
   where starttime > '2010-05-12' and endtime < '2010-05-13')
Select CAST(AVG(CAST(TotalTime As Float))As Datetime) 
  from TransTime

I'm getting the following result:

1900-01-01 00:00:00.007

I can't figure out how to strip the date off and just display the time, 00:00:00:007. Any help would be appreciated. Thank you.

A: 

You probably want to use the DATEPART function to do this. Check out the documentation here.

Brian Sullivan
A tad confusing, but I'm reading through that documentation now.
+4  A: 

You want to cast as a TIME.

With TransTime AS (
  select endtime-starttime AS Totaltime 
    from transactiontime
   where starttime > '2010-05-12' and endtime < '2010-05-13')
Select CAST(AVG(CAST(TotalTime As Float))As TIME(7)) 
  from TransTime
Scott Chamberlain
That gives me the following:Msg 529, Level 16, State 2, Line 1Explicit conversion from data type float to time is not allowed.
+1  A: 

It's that first subtraction that's your problem, and why are you casting the result to DATETIME (or even TIME)?

With TransTime AS 
(
    -- get time in milliseconds
    select DATEDIFF(ms, starttime, endtime) AS Totaltime 
        from transactiontime
        where starttime > '2010-05-12' and endtime < '2010-05-13'
)
Select AVG(CAST(TotalTime As Float)) AS average_time_in_msec
    FROM TransTime
egrunin
That's why I started coming to this site! That did the trick. Hadn't thought about Datediff. Thank you for the help.