[英]Operand type clash: date is incompatible with int solved with CONVERT() … but WHY?
My question is about WHY? 我的问题是关于为什么? WHAT is SQL doing to cause this error message in the situation below. 在以下情况下,SQL会怎么做导致此错误消息。
There are a few of these queries on stack but usually because of a missing DateAdd(). 堆栈中有一些这样的查询,但是通常是因为缺少DateAdd()。 Here I am simply trying to put the date passed in as part of an audit note string against the record. 在这里,我只是试图将传递的日期作为记录的审计注释字符串的一部分。 I am not specifically asking the two variables to play together and give me a result. 我并不是专门要求两个变量一起发挥作用并给出结果。
Below is the line of issue. 下面是问题所在。 At the bottom the fuller query for context. 在底部,对上下文进行了更全面的查询。
@Days INT, @Date DATE @Days INT,@ DATE DATE
'Extension to claim of ' + @Days + ' from ' + @Date + '.' '从'+ @Date +'扩展为要求'+ @Days +'。 , ,
SELECT TOP 1
ID,
WorkFlowStatusCode ,
'Number of days: ' + @Days + ' from ' + @Date + '.' ,
@UserId ,
@UserId
FROM dbo.Table
WHERE ID= @VariableA
ORDER BY ID DESC;
What I have found is that wrapping the @Date in a Convert() to 103 solves the issue. 我发现的是,将@Date包裹在Convert()中,可以解决103问题。 See Below. 见下文。
'Extension to claim of ' + @Days + ' from ' + CONVERT(NVARCHAR(10),@Date,103) + '.' '从'+ CONVERT(NVARCHAR(10),@ Date,103)+'到'+ @Days +'的扩展要求。 , ,
I am simply trying to understand why I would need to do that in this situation from SQL/Database point of view. 我只是想从SQL /数据库的角度理解为什么在这种情况下需要这样做。 I can't find that answer online to help my understanding. 我找不到在线答案来帮助我理解。
All comments greatly appreciated. 所有评论非常感谢。
Lee 背风处
Presumably @Day
is an integer. 大概@Day
是一个整数。 Remember that +
can mean both addition and string concatenation, so SQL can get confused. 请记住, +
表示加法和字符串连接,因此SQL可能会造成混淆。 Try this: 尝试这个:
'Number of days: ' + convert(varchar(255), @Days) + ' from ' + convert(varchar(10), @Date, 103) + '.'
Notes: 笔记:
varchar()
or nvarchar()
, always include a length. 使用varchar()
或nvarchar()
,请始终包含长度。 SQL Server has a default length that varies by context. SQL Server的默认长度因上下文而异。 Not including the length is an easy way to create a very-hard-to-debug error. 不包括长度是创建很难调试的错误的简便方法。 varchar()
rather than nvarchar()
because the rest of the string are not national character sets. 您的代码建议使用varchar()
而不是nvarchar()
因为字符串的其余部分都不是国家字符集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.