简体   繁体   中英

Datediff in minutes using Field and literal String

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 ie both are column values or literal strings.

Here is the 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

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]
select datediff(minute,cast (enquiry as time),cast('17:00:00' as time)) from test_dates as t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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