SQL DateDiff 周 - 需要和替代方案
MS SQL DateDiff 函数在计算两个日期之间的差异时计算跨越边界的数量。
对我来说不幸的是,那不是我所追求的。例如,2012 年 6 月 1 日 -> 2012 年 6 月 30 日跨越 4 个边界,但涵盖 5 周。
我可以运行另一个查询来计算一个月相交的周数吗?
更新
为了尝试准确地澄清我所追求的内容:
对于任何给定的月份,我需要与该月相交的周数。
另外,对于仅采用 datediff 并添加一个的建议,这是行不通的。例如,2010 年 2 月仅相交 4 周。 DateDiff
调用返回 4,这意味着简单地添加 1 就会导致错误的周数。
The MS SQL DateDiff function counts the number of boundaries crossed when calculating the difference between two dates.
Unfortunately for me, that's not what I'm after. For instance, 1 June 2012 -> 30 June 2012 crosses 4 boundaries, but covers 5 weeks.
Is there an alternative query that I can run which will give me the number of weeks that a month intersects?
UPDATE
To try and clarify exactly what I'm after:
For any given month I need the number of weeks that intersect with that month.
Also, for the suggestion of just taking the datediff and adding one, that won't work. For instance February 2010 only intersects with 4 weeks. And the DateDiff
calls returns 4, meaning that simply adding 1 would leave me the wrong number of weeks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:正确的周计算通常比您想象的要复杂!
如果您使用
Datepart(week, aDate)
,您会对“周”概念做出很多假设。一周是从周日还是周一开始?您如何处理第一周和第五周之间的过渡。一年中的实际周数会有所不同,具体取决于您使用的周计算规则(first4dayweek、weekOfJan1 等),
如果您只是想处理差异,则可以
在第一个日期时间位于
2011-01-01 时 使用15:43:22
那么差异是2011-02-05 15:43:22
之后 5 周编辑: 实际上,根据这篇文章:< a href="https://stackoverflow.com/questions/2220786/wrong-week-number-using-datepart-in-sql-server">在 SQL Server 中使用 DATEPART 的周数错误
您现在可以使用
Datepart(isoww, aDate)
来获取ISO 8601周数。我知道week
被破坏了,但不知道现在有修复。凉爽的!如果您使用星期一作为一周的第一天,则此方法有效
英国
,您可以将星期一设置为一周的第一天。isoww
参数使用星期一作为一周的第一天,但它会导致 1 月有时在第 52/53 周开始,12 月有时在第 1 周结束(这会使您的 select 语句更加复杂)Beware: Proper Week calculation is generally trickier than you think!
If you use
Datepart(week, aDate)
you make a lot of assumptions about the concept 'week'.Does the week start on Sunday or Monday? How do you deal with the transition between week 1 and week 5x. The actual number of weeks in a year is different depending on which week calculation rule you use (first4dayweek, weekOfJan1 etc.)
if you simply want to deal with differences you could use
if the first dateTime is at
2011-01-01 15:43:22
then the difference is 5 weeks after2011-02-05 15:43:22
EDIT: Actually, according to this post: Wrong week number using DATEPART in SQL Server
You can now use
Datepart(isoww, aDate)
to get ISO 8601 week number. I knew thatweek
was broken but not that there was now a fix. Cool!THIS WORKS if you are using monday as the first day of the week
british
you make monday the first day of the week.isoww
parameter uses monday as the first day of the week, but it causes january to sometimes start in week 52/53 and december to sometimes end in week 1 (which would make your select statement more complex)SET DATEFIRST 在计算周数时非常重要。要检查您拥有的内容,可以使用选择@@datefirst。 @@datefirst=7 表示一周的第一天是星期日。
结果为 5,因为 2010 年 2 月 28 日星期日是第五周的第一天。
如果您想根据每周的第一天是星期一来计算周数,则需要这样做。
结果是 4。
SET DATEFIRST is important when counting weeks. To check what you have you can use select @@datefirst. @@datefirst=7 means that first day of week is sunday.
Result is 5 because Sunday 28/2 - 2010 is the first day of the fifth week.
If you want to base your week calculations on first day of week is Monday you need to do this instead.
Result is 4.