使用Field和literal String在几分钟内完成Datediff

时间:2020-12-21 01:36:03

I am having issues trying to calculate the difference in Minutes between a Field and a literal string. However the output I am getting doesnt seem right at all, it looks to me like you can't seem to compare a Field and a literal string, however if they both the same then it seems fine i.e both are column values or literal strings.

我在尝试计算字段和文字字符串之间的分钟差异时遇到问题。然而,我得到的输出似乎并不正确,它看起来像你似乎无法比较字段和文字字符串,但如果它们都相同然后它似乎很好,即两者都是列值或文字字符串。

Here is the SQL

这是SQL

select datediff(minute,enquiry,cast('17:00:00' as time))

from [dbo].[Test_Dates]

Is there anyway to get the right results, given the enquiry Field has values of hours within business hours. The result from the above query I am getting is as follows: -61536240

无论如何都能得到正确的结果,因为查询字段在营业时间内具有小时值。我得到的上述查询的结果如下:-61536240

2 个解决方案

#1


0  

You should make both fields in time format

您应该以时间格式制作两个字段

SELECT DATEDIFF(minute, CONVERT(CHAR(5), enquiry, 108) , CAST('17:00:00' as time))
  FROM [dbo].[Test_Dates]

or

要么

SELECT DATEDIFF(minute, CAST(enquiry as time) , CAST('17:00:00' as time))
  FROM [dbo].[Test_Dates]

#2


0  

select datediff(minute,cast (enquiry as time),cast('17:00:00' as time)) from test_dates as t

#1


0  

You should make both fields in time format

您应该以时间格式制作两个字段

SELECT DATEDIFF(minute, CONVERT(CHAR(5), enquiry, 108) , CAST('17:00:00' as time))
  FROM [dbo].[Test_Dates]

or

要么

SELECT DATEDIFF(minute, CAST(enquiry as time) , CAST('17:00:00' as time))
  FROM [dbo].[Test_Dates]

#2


0  

select datediff(minute,cast (enquiry as time),cast('17:00:00' as time)) from test_dates as t