我坚持这个DATEDIFF

时间:2017-01-16 17:10:48

标签: sql-server tsql datediff

好的,所以我一直收到这个错误:

  

Msg 245,Level 16,State 1,Line 1   转换varchar值时转换失败'天'数据类型int。

下面是我的代码.....我在这里做错了什么....我试图显示日,小时和分钟差异...我在我的约会中使用了CASTS因为那些是不同的列所以我使用演员来结合他们,以便更容易做我的约会.......

DATEDIFF(DD, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME) + CAST(txt_signoff_ml_time AS DATETIME)) + ' Days ' 
        + DATEDIFF(HH, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME) + CAST(txt_signoff_ml_time AS DATETIME)) + ' Hours '
        + DATEDIFF(MINUTE, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME) + CAST(txt_signoff_ml_time AS DATETIME)) + ' Minutes'
AS Time_diff2,

2 个答案:

答案 0 :(得分:2)

DateDiff函数将以整数形式返回结果。在这里,您将结合Integer值和字符串值。因此,您将面临类型差异错误。

您必须将datediff输出转换为varchar,如下所述

cast(DATEDIFF(DD, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME) + CAST(txt_signoff_ml_time AS DATETIME))as varchar(50)) + ' Days ' 
        + cast(DATEDIFF(HH, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME) + CAST(txt_signoff_ml_time AS DATETIME))as varchar(50)) + ' Hours '
        + cast(DATEDIFF(MINUTE, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME) + CAST(txt_signoff_ml_time AS DATETIME)) as varchar(50)) + ' Minutes'

答案 1 :(得分:0)

此:

DATEDIFF(DD, checkin_datetime, CAST(txt_signoff_ml_date AS DATETIME)

需要一个结束括号。这应该让你开始。