在 SQL 中与 Dateiff 作斗争

发布于 2024-12-04 19:44:19 字数 386 浏览 1 评论 0原文

我正在用 SQL 编写一个小查询,并且遇到了一个似乎有人以前遇到过的问题。我想找出两个日期之间的月数。我正在使用像 ... 这样的表达式,

DATEDIFF(m,{firstdate},{seconddate})

但是我注意到这个函数正在计算日期超过每月阈值的次数。例如...

DATEDIFF(m,3/31/2011,4/1/2011) will yield 1  
DATEDIFF(m,4/1/2011,4/30/2011) will yield 0 
DATEDIFF(m,3/1/2011,4/30/2011) will yield 1

有谁知道如何根据经过的时间然后超过每月阈值的时间来查找两个日期之间的月份?

I am writing a little query in SQL and am butting heads with an issue that it seems like someone must have run into before. I am trying to find the number of months between two dates. I am using an expression like ...

DATEDIFF(m,{firstdate},{seconddate})

However I notice that this function is tallying the times the date crosses the monthly threshold. In example...

DATEDIFF(m,3/31/2011,4/1/2011) will yield 1  
DATEDIFF(m,4/1/2011,4/30/2011) will yield 0 
DATEDIFF(m,3/1/2011,4/30/2011) will yield 1

Does anyone know how to find the months between two dates more-so based upon time passed then times passed the monthly threshold?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私藏温柔 2024-12-11 19:44:19

如果您想查找名义上的月份数,为什么不查找天数差异,然后除以 30(根据需要转换为 FLOAT)。或者 30.5 左右 - 取决于您想要如何处理全年可变的月份长度。但也许这不是您的具体情况的一个因素。

If you want to find some notional number of months, why not find the difference in days, then divide by 30 (cast to FLOAT as required). Or 30.5-ish perhaps - depends on how you want to handle the variable month length throughout the year. But perhaps that's not a factor in your particular case.

御守 2024-12-11 19:44:19

以下语句具有相同的开始日期和相同的结束日期。这些日期相邻且时间相差 0.0000001 秒。每个语句中的开始日期和结束日期之间的差异跨越其日期部分的一个日历或时间边界。每个语句返回 1。 ...
选择 DATEDIFF(月, '2005-12-31 23:59:59.9999999'
, '2006-01-01 00:00:00.0000000'); ....

(来自 DATEDIFFdatepart 边界< /强>)。如果您对此不满意,您可能需要按照 ma​​rtinclayton 的建议使用天作为单位

The following statements have the same startdate and the same endate. Those dates are adjacent and differ in time by .0000001 second. The difference between the startdate and endate in each statement crosses one calendar or time boundary of its datepart. Each statement returns 1. ...
SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999'
, '2006-01-01 00:00:00.0000000'); ....

(from DATEDIFF, section datepart Boundaries ). If you are not satisfied by it, you probably need to use days as unit as proposed by martin clayton

小梨窩很甜 2024-12-11 19:44:19
DATEDIFF(m,{firstdate},ISNULL({seconddate},GETDATE())) - CASE 
                                                         WHEN DATEPART(d,{firstdate}) >= DATEPART(d,ISNULL({seconddate},GETDATE()))
                                                         THEN 1
                                                         ELSE 0
DATEDIFF(m,{firstdate},ISNULL({seconddate},GETDATE())) - CASE 
                                                         WHEN DATEPART(d,{firstdate}) >= DATEPART(d,ISNULL({seconddate},GETDATE()))
                                                         THEN 1
                                                         ELSE 0
薄暮涼年 2024-12-11 19:44:19

DATEDIFF 的设计就是这样的。在评估特定时间测量值(例如月或天等)时,它仅考虑该测量值和较高的值 - 忽略较小的值。任何时间测量都会遇到这种行为。例如,如果您使用 DATEDIFF 来计算天数,并且一个日期在午夜前几秒,另一个日期在午夜后几秒,您将得到“1”天的差异,即使这两个日期只有几秒。相隔几秒。

DATEDIFF 旨在给出问题的粗略答案,如下所示:

问题:您多大了?
答案:某个整数。你不会说“我今年59岁4个月17天5小时35分27秒”。你只要说“我59岁了”。这也是 DATEDIFF 的方法。

如果您想要一个根据上下文含义量身定制的答案(例如您的儿子说“我不是 8 岁!我已经 8 又四分之三了!或者我几乎 9 岁了!),那么你应该查看下一个最小的测量值并用它进行近似,所以如果你是在几个月之后,那么在天或小时上做一个 DATEDIFF ,并尝试近似几个月,但它似乎与你的情况最相关(也许你)。想要答案例如 1-1/2 个月,或 1.2 个月等)使用 CASE / IF-THEN 类型的逻辑。

DATEDIFF is like this by design. When evaluating a particular time measurement (like months, or days, etc.), it considers only that measurement and higher values -- ignoring smaller ones. You'll run into this behavior with any time measurement. For example, if you used DATEDIFF to calculate days, and had one date a few seconds before midnight, and another date a few seconds after midnight, you'd get a "1" day difference, even though the two dates were only a few seconds apart.

DATEDIFF is meant to give a rough answer to questions, like this:

Question: how many years old are you?
Answer: some integer. You don't say "I'm 59 years, 4 months, 17 days, 5 hours, 35 minutes and 27 seconds old". You just say "I'm 59 years old". That's DATEDIFF's approach too.

If you want an answer that's tailored to some contextual meaning (like your son who says "I'm not 8! I'm 8 and 3-quarters!, or I'm almost 9!), then you should look at the next-smallest measurement and approximate with it. So if it's months you're after, then do a DATEDIFF on days or hours instead, and try to approximate months however it seems most relevant to your situation (maybe you want answers like 1-1/2 months, or 1.2 months, etc.) using CASE / IF-THEN kinds of logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文