简体   繁体   English

操作数类型冲突:时间与int不兼容

[英]Operand type clash: time is incompatible with int

In a table, I have a column (AvgWaitTime) which stores data value in seconds (Data Type: float), date wise. 在表中,我有一列(AvgWaitTime),以日期为单位,以秒(数据类型:float)存储数据值。 I have a function which performs some calculation using AvgWaitTime column and other few columns and returns a value in time format. 我有一个函数,该函数使用AvgWaitTime列和其他几列执行一些计算,并以时间格式返回一个值。 I would to convert the value returned by the function (time format) into seconds (preferrably decimal, if not then int). 我会将函数(时间格式)返回的值转换为秒(最好是十进制,如果不是,则为int)。

select 
(datepart(HH, dbo.fnGetMonthlyAverageWaitTime(m.RDate) * 60 * 60) +
datepart(mi, dbo.fnGetMonthlyAverageWaitTime(m.RDate) * 60) + 
datepart(s, dbo.fnGetMonthlyAverageWaitTime(m.RDate)))[MonthlyAverageWaitTime]
from TelephonyMTD m

Error: Operand type clash: time is incompatible with int

So, I tried to run this: 因此,我尝试运行此命令:

 select 
(datepart(HH, GetDate() * 60 * 60) +
datepart(mi, GetDate() * 60) + 
datepart(s, GetDate()))

Now it says, Implicit conversion from data type datetime to int is not allowed. 现在说,不允许从数据类型datetime到int的隐式转换。 Use the CONVERT function to run this query. 使用CONVERT函数运行此查询。 Which is true when I looked at the data type conversion chart, I came to know that conversion to int and float is now allowed. 当我查看数据类型转换图表时,这是事实,我知道现在允许转换为int和float。

Please advice. 请指教。

The problem is you are trying to multiply a date/datetime by an integer which doesn't make sense: 问题是您试图将日期/日期时间乘以没有意义的整数:

GetDate() * 60 * 60

You could simply use DATEDIFF with seconds, to get the value in seconds: 您可以简单地将DATEDIFF与秒配合使用,以秒为单位获取值:

SELECT  DATEDIFF(SECOND, '00:00:00', dbo.fnGetMonthlyAverageWaitTime(m.RDate)) AS MonthlyAverageWaitTime
FROM    TelephonyMTD AS m

QUICK EXAMPLE 快速示例

SELECT  t.AvgTime, 
        AvgTimeInSeconds = DATEDIFF(SECOND, '00:00:00', t.AvgTime)
FROM    (VALUES
            (CAST('00:01:15' AS TIME)),
            (CAST('05:36:47' AS TIME))
        ) AS t (AvgTime);

Which gives: 这使:

+----------+------------------+
|  AvgTime | AvgTimeInSeconds |
+----------+------------------+
| 00:01:15 |        75        |
| 05:36:47 |       20207      |
+----------+------------------+

Open and Close brackets are the one causing issue for the above error: Please try like below 开括号和右括号是导致上述错误的一个原因:请按以下方式尝试

SELECT ( 
( Datepart(HH, Getdate()) * 60 * 60 ) + 
( Datepart(mi, Getdate()) * 60 ) + 
Datepart(s, Getdate()) 
) 

Try to use Below Syntax to convert seconds to day, hour, minute, seconds 尝试使用“以下语法”将seconds转换为day, hour, minute, seconds

DECLARE @sec INT = 86400;

SELECT 
     CAST(@sec /60/60/24 AS VARCHAR(12))  + ' Days,' 
  +  CAST(@sec /60/60 % 24 AS VARCHAR(12))+ ' Hours,'
  +  CAST(@sec /60 % 60 AS VARCHAR(2))    + ' Minutes, ' 
  +  CAST(@sec % 60 AS VARCHAR(2))        + ' Seconds.';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM