使用 DateDiff 时的动态 DatePart
有没有办法将 DateDiff 的 DatePart 参数作为变量传递? 这样我就可以编写与此类似的代码?
DECLARE @datePart VARCHAR(2)
DECLARE @dateParameter INT
SELECT @datePart = 'dd'
SELECT @dateParameter = 28
SELECT
*
FROM
MyTable
WHERE
DATEDIFF(@datePart, MyTable.MyDate, GETDATE()) < @dateParameter
我能想到的唯一方法是使用 CASE 语句检查参数的值,或者将 SQL 构建为字符串并在 EXEC 中运行它。
有人有任何“更好”的建议吗? 平台是MS SQL Server 2005
Is there a way to pass the DatePart parameter of DateDiff as a variable?
So that I can write code that is similar to this?
DECLARE @datePart VARCHAR(2)
DECLARE @dateParameter INT
SELECT @datePart = 'dd'
SELECT @dateParameter = 28
SELECT
*
FROM
MyTable
WHERE
DATEDIFF(@datePart, MyTable.MyDate, GETDATE()) < @dateParameter
The only ways I can think of doing it are with a CASE statement checking the value of the parameter or by building the SQL as a string and running it in an EXEC.
Does anyone have any "better" suggestions? The platform is MS SQL Server 2005
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不幸的是旧的但仍然有效。
我按照案例的方式做了,只是想分享代码,这样你就不必做我必须做的所有烦人的打字。 涵盖所有可能的日期部分。 只需替换函数名称和日期函数即可为其他 T-SQL 日期函数实现。
复制并粘贴部分
Old but still valid unfortunately.
I did it the case way and just want to share the code so you don't have to do all the annoying typing I had to do. Covers all possible date parts. Just replace the name of the function and the date function to implement for other T-SQL date functions.
Copy and paste section
根据 DATEDIFF 上的 BOL 条目(对于 SQL Server 2005,参数部分),
因此,您可能会陷入动态 SQL 或使用 CASE 语句的困境。
但我会选择 CASE 版本而不是动态 SQL。
According to BOL entry on DATEDIFF (arguments section) for SQL Server 2005,
So you are probably stuck with Dynamic SQL or using a CASE statement.
But I would opt for a CASE version instead of dynamic SQL.
除了建议的动态 sql 或 case 语句之外,您真正可以做的唯一一件事就是始终在粒度 DatePart 处执行 datediff,然后进行上转换。 但这并不是万无一失的,如果您尝试使用 datediff 在太大的跨度上细化某个部分,例如 datediff(second, 0, getdate()),您将在函数中遇到溢出。 但如果你只需要像微小部分这样的东西,你应该没问题(仔细检查你关心的最大日期值)。
例如,
如果我想将其转换为小时、天等,我可以将结果除以适当的数量。 它不会考虑闰年等。
The only thing you can really do aside from the suggested dynamic sql or case statement is to always do the datediff at a granular DatePart and then upconvert. This isn't fool proof though, you will get an overflow in the function if you try to datediff to granular a part over too large a span e.g. datediff(second, 0, getdate()). But if you just need something like minute parts you should be fine (double check with max date values you care about).
So for example
If I want to convert this to hours, days, etc, I can just divide the result by the appropriate amount. It won't take into account leap years etc.
当然,使用如下所示的动态查询效果很好,我假设您计划仅使用 @datePart 的缩写。 我建议对日期部分至少使用 VARCHAR(4),这将处理 yyyy 和 mcs 等缩写。 快乐编码:
Definitely using the dynamic query such as this below works very well, I assume you plan on using only abbreviation for @datePart. I would recommend using a minimum of VARCHAR(4) for datepart this will handle such abbreviations as yyyy and mcs. Happy coding:
我认为没有比你描述的更好的方法了。 Sql Server 可能会将 DATEDIFF 查询编译为一组依赖于 datepart 参数的操作。 所以你需要一个 CASE,或者动态查询。
I don't think there are better ways then you describe. Sql Server probably compiles the DATEDIFF query to a set of operations that depend on the datepart parameter. So you'd need a CASE, or dynamic queries.
我创建了一个标量值函数来执行此操作。 它基于上面提到的解决方案,但更容易在代码中实现
在查询中,您可以像这样使用此函数:
I created a Scalar values function to do this. It is based on the solutions mentioned above but easier to implement in your code
In a query you can use this function like this: