在查询的DATEADD函数中使用参数 [英] Using Parameters in DATEADD function of a Query

查看:241
本文介绍了在查询的DATEADD函数中使用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的SQL中使用SQL的 DateAdd 函数询问.问题是,当我使用参数设置第二个论点时, number 参数出现一个错误,该错误将表示如下内容:

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:

无法从中转换参数值 十进制到DateTime

Failed to convert parameter value from a Decimal to a DateTime

虽然我将其输入为无参数,即对Int进行硬编码,但效果很好.

While if I enter it parameterless, i.e hardcode an Int, it works fine.

这有效:

SELECT     FieldOne, DateField
FROM         Table
WHERE     (DateField> DATEADD(day, -10, GETDATE()))

而事实并非如此:

SELECT     FieldOne, DateField
FROM         Table
WHERE     (DateField> DATEADD(day, @days, GETDATE()))

@days = -10

Where @days = -10

有什么想法让我做错了吗?顺便说一句,我正在SQL Server Manager中设置此变量,因为我正在尝试找出DataAccess代码中的错误.不知道这是否有所作为.

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.

谢谢

推荐答案

听起来您要将小数作为第3个参数而不是第2个参数传递给DATEADD(),例如:

It sounds like you're passing the decimal as the 3rd instead of the 2nd parameter to DATEADD(), like:

DATEADD(day, GETDATE(), @days)

尽管问题中的代码段看起来不错.

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.)

这篇关于在查询的DATEADD函数中使用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆