在查询的 DATEADD 函数中使用参数
我正在尝试在查询中使用 SQL 的 DateAdd 函数。问题是当我使用参数设置第二个参数 number 参数时,我收到一个错误,该错误将显示如下内容:
无法转换参数值 小数转换为日期时间
虽然如果我无参数输入它,即硬编码一个 Int,它工作正常。
这有效:
SELECT FieldOne, DateField
FROM Table
WHERE (DateField> DATEADD(day, -10, GETDATE()))
而这无效:
SELECT FieldOne, DateField
FROM Table
WHERE (DateField> DATEADD(day, @days, GETDATE()))
Where @days = -10
对我做错了什么有什么想法吗?顺便说一句,我正在 SQL Server Manager 中设置此变量,因为我正在尝试解决 DataAccess 代码中的错误。不确定这是否有影响。
谢谢
I am trying to us the DateAdd function of SQL in my Query. The problem is when I use a parameter to set the second arguement, the number argument I get an error which will say something like this:
Failed to convert parameter value from
a Decimal to a DateTime
While if I enter it parameterless, i.e hardcode an Int, it works fine.
This works:
SELECT FieldOne, DateField
FROM Table
WHERE (DateField> DATEADD(day, -10, GETDATE()))
while this does not:
SELECT FieldOne, DateField
FROM Table
WHERE (DateField> DATEADD(day, @days, GETDATE()))
Where @days = -10
Any ideas into what I am doing wrong? Incidentally I am setting this variable in SQL Server Manager, as I am trying to work out a bug in my DataAccess code. Not sure if that makes a difference.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一篇旧帖子,但对于遇到此问题的其他人来说,我在 Reporting Services 2008 R2 中也遇到了类似的问题,尽管错误消息是“参数数据类型 nvarchar 对于 dateadd 函数的参数 2 无效”。我认为这个问题可能是相关的。
该问题是由 Reporting Services 解析 SQL 代码以生成报表数据集的方式引起的。就我而言,我能够将此数据集查询:更改
为:
并且错误已解决。
编辑:只是稍微扩展一下这个答案:问题是 Reporting Services 无法解析
@NumWeeks
的正确数据类型,我认为可能是因为它位于DateAdd( )
函数,并默认为 NArchar。添加显式Convert()
将数据类型设置为 Int(即使它已经是数字)使解析器能够正确识别@NumWeeks
的数据类型。I know this is an old post, but for anyone else having this problem I had a similar issue in Reporting Services 2008 R2, although the error message was "Argument data type nvarchar is invalid for argument 2 of dateadd function." I think this issue could be related.
The problem was caused by the way Reporting Services parses the SQL code to generate a report dataset. In my case, I was able to change this dataset query:
to this:
and the error was resolved.
EDIT: Just to expand on this answer a little: the issue was that Reporting Services was unable to parse the correct data type for
@NumWeeks
, I think possibly due to it being inside theDateAdd()
function, and was defaulting it to NVarchar. Adding an explicitConvert()
to set the data type to Int (even though it was already a number) enabled the parser to correctly identify the data type for@NumWeeks
.听起来您将小数作为第三个参数而不是第二个参数传递给
DATEADD()
,例如:尽管问题中的代码片段看起来不错。
(为了更加清楚起见,上面的代码片段是一个错误。这是将从问题中生成错误的代码。)
It sounds like you're passing the decimal as the 3rd instead of the 2nd parameter to
DATEADD()
, like:Although the snippet in the question looks fine.
(For extra clarity, the snippet above is an error. This is the code that would generate the error from the question.)
下面的代码在这里工作得很好(SQL Server 2005,在 Management Studio 中执行):
如下所示
所以,问题一定出在其他地方......
The following code works perfectly fine here (SQL Server 2005, executed in Management Studio):
as does the following
So, the problem must lie somewhere else...
您确定该错误与此语句相关吗?不涉及小数,如果我尝试这样做,它仍然有效
即使尝试将 -10 小数转换为smalldatetime,这也会产生不同的错误
Are you sure the error is associated with this statement? There are no decimals involved and if I try this it still works
Even trying to cast -10 decimal to smalldatetime this gives a different error