简体   繁体   English

通过在 SQL 服务器中组合 DATEDIFF(hh,StartTime,EndTime) + 'Minutes' 需要 output

[英]Need output by combining DATEDIFF(hh,StartTime,EndTime) + 'Minutes' in SQL Server

When running this query, I need output by combining运行此查询时,我需要 output 通过组合

DATEDIFF(mm, StartTime, EndTime) + 'Minutes'

I'm getting this error我收到此错误

Conversion failed when converting the varchar value 'Minutes' to data type int将 varchar 值“分钟”转换为数据类型 int 时转换失败

I need to achieve the output like 15 Minutes .我需要像15 Minutes一样达到 output 。

(15 represents the difference between StartTime and EndTime) (15代表StartTime和EndTime的区别)

DATEDIFF function returns a INT values. DATEDIFF function 返回一个 INT 值。 So you need to CAST it to a string.所以你需要把它转换成一个字符串。

Try something like尝试类似的东西

SELECT CAST(DATEDIFF(mm,StartTime, EndTime) AS NVARCHAR(2)) + 'Minutes'

Obviously you can use other destination string type like char\nchar etc... And use the CONVERT function instead of CAST.显然,您可以使用其他目标字符串类型,如 char\nchar 等...并使用 CONVERT function 而不是 CAST。

Use CONCAT() :使用CONCAT()

select concat(DATEDIFF(minute, StartTime, EndTime), 'Minutes')

CONCAT() will conveniently convert arguments to strings. CONCAT()将方便地将 arguments 转换为字符串。 Also note that this spells out minute .另请注意,这说明了minute I recommend using the full name when using date functions.我建议在使用日期函数时使用全名。

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

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