SQL DateDiff 周 - 需要和替代方案

发布于 2024-10-31 13:03:52 字数 365 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

蝶…霜飞 2024-11-07 13:03:52

注意:正确的周计算通常比您想象的要复杂!

如果您使用 Datepart(week, aDate),您会对“周”概念做出很多假设。
一周是从周日还是周一开始?您如何处理第一周和第五周之间的过渡。一年中的实际周数会有所不同,具体取决于您使用的周计算规则(first4dayweek、weekOfJan1 等),

如果您只是想处理差异,则可以

DATEDIFF('s', firstDateTime, secondDateTime) > (7 * 86400 * numberOfWeeks)

在第一个日期时间位于 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 被破坏了,但不知道现在有修复。凉爽的!

如果您使用星期一作为一周的第一天,则此方法有效

 set language = british
 select datepart(ww, @endofMonthDate) - 
        datepart(ww, @startofMonthDate) + 1 
  1. 日期部分为语言敏感。通过将语言设置为英国,您可以将星期一设置为一周的第一天。
  2. 这将返回 2010 年 2 月和 2012 年 6 月的正确值! (因为星期一而不是星期日是一周的第一天)。
  3. 它似乎还返回一月和十二月的正确周数(无论年份)。 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

DATEDIFF('s', firstDateTime, secondDateTime) > (7 * 86400 * numberOfWeeks)

if the first dateTime is at 2011-01-01 15:43:22 then the difference is 5 weeks after 2011-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 that week 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

 set language = british
 select datepart(ww, @endofMonthDate) - 
        datepart(ww, @startofMonthDate) + 1 
  1. Datepart is language sensistive. By setting language to british you make monday the first day of the week.
  2. This returns the correct values for feburary 2010 and june 2012! (because of monday as opposed to sunday is the first day of the week).
  3. It also seems to return correct number of weeks for january and december (regardless of year). The 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)
辞慾 2024-11-07 13:03:52

SET DATEFIRST 在计算周数时非常重要。要检查您拥有的内容,可以使用选择@@datefirst。 @@datefirst=7 表示一周的第一天是星期日。

set datefirst 7

declare @FromDate datetime = '20100201'
declare @ToDate datetime = '20100228'

select datepart(week, @ToDate) - datepart(week, @FromDate) + 1

结果为 5,因为 2010 年 2 月 28 日星期日是第五周的第一天。

如果您想根据每周的第一天是星期一来计算周数,则需要这样做。

set datefirst 1

declare @FromDate datetime = '20100201'
declare @ToDate datetime = '20100228'

select datepart(week, @ToDate) - datepart(week, @FromDate) + 1

结果是 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.

set datefirst 7

declare @FromDate datetime = '20100201'
declare @ToDate datetime = '20100228'

select datepart(week, @ToDate) - datepart(week, @FromDate) + 1

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.

set datefirst 1

declare @FromDate datetime = '20100201'
declare @ToDate datetime = '20100228'

select datepart(week, @ToDate) - datepart(week, @FromDate) + 1

Result is 4.

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