Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 SQL计算平均时间,但给定时间为NVARCHAR格式_Sql Server 2008 - Fatal编程技术网

Sql server 2008 SQL计算平均时间,但给定时间为NVARCHAR格式

Sql server 2008 SQL计算平均时间,但给定时间为NVARCHAR格式,sql-server-2008,Sql Server 2008,我在将nvarchar转换为int时遇到了一些问题 我想做的是得到一列的最长和最短时间,然后计算平均时间,但是该列是nvarchar。您在找这个吗 Set Nocount On; If Object_Id('tempdb.dbo.#sometable') Is Not Null Begin Drop Table #sometable; End If Object_Id('tempdb.dbo.#computetable') Is Not Null Begin Drop Tab

我在将nvarchar转换为int时遇到了一些问题


我想做的是得到一列的最长和最短时间,然后计算平均时间,但是该列是nvarchar。

您在找这个吗

Set Nocount On;

If Object_Id('tempdb.dbo.#sometable') Is Not Null
Begin
    Drop Table #sometable;
End

If Object_Id('tempdb.dbo.#computetable') Is Not Null
Begin
    Drop Table #computetable;
End

Create Table #sometable
(
     RowId          Int Identity(1,1) Primary Key
    ,TimeTaken      Nvarchar(8)
)

Create Table #computetable
(
     RowId          Int Primary Key
    ,ComputeTime    Int
)

Insert Into #sometable(TimeTaken) Values
('15:24:58'),('5:20:08'),('10:14:50'),('21:02:01'),('4:27:30'),('16:21:45')

Insert Into #computetable(RowId,ComputeTime)
Select   st.RowId
        ,(st.HoursToSeconds + st.MinutesToSeconds + st.Seconds) As ComputeTime
From    (
            Select   st.RowId
                    ,(st.Hours * 3600) As HoursToSeconds
                    ,(Cast(Left(st.TimeTaken, (Charindex(':', st.TimeTaken, 0) - 1)) As Int) * 60) As MinutesToSeconds
                    ,Cast(Right(st.TimeTaken, (Charindex(':', st.TimeTaken, 0) - 1)) As Int) As Seconds
            From    (
                        Select   st.RowId
                                ,Cast(Left(st.TimeTaken, (Charindex(':', st.TimeTaken, 0) - 1)) As Int) As Hours
                                ,Substring(st.TimeTaken, (Charindex(':', st.TimeTaken, 0) + 1), Len(st.TimeTaken)) As TimeTaken
                        From    #sometable As st With (Nolock)
                    ) As st
        ) As st

Select   Max(ct.ComputeTime) As HighestTime
        ,Min(ct.ComputeTime) As LowestTime
        ,Avg(ct.ComputeTime) As AvgTime
From    #computetable As ct With (Nolock)

你能举例说明数据的格式吗?是这样的00:00:00你试过
CAST
CONVERT
?当您显示格式00:00:00时,我们是否可以假定它不应超过23小时?