开发者

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 writi开发者_Go百科ng 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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜