简体   繁体   English

如何从日期列中减去一个月

[英]How to subtract one month from a Date Column

I know about Dateadd and datediff, but I cannot find any information how to use these functions on an actual date column rather than something like today's date with SQL Server. 我知道Dateadd和datediff,但我找不到任何有关如何在实际日期列上使用这些函数的信息,而不是像今天的SQL Server日期那样。

Say I have the following Column 说我有以下列

Dated
06/30/2015
07/31/2015

Now I want to add the following derived column that subtracts one month from every row in the Dated column. 现在我想添加以下派生列,该列从Dated列中的每一行减去一个月。

Dated             Subtracted
06/30/2015        05/31/2015
07/31/2015        06/30/2015

Thank you 谢谢

Why don't you just get the last day of the previous month? 你为什么不在上个月的最后一天? If this solve your problem, here's the sql server syntax, just replace the variable @yourDate with your column name. 如果这解决了你的问题,这里是sql server语法,只需用你的列名替换变量@yourDate。

DECLARE @yourDate DATE = '20160229'
select DATEADD(MONTH, DATEDIFF(MONTH, -1, @yourDate)-1, -1)

The short answer: I suspect this is what you want: 简短的回答:我怀疑这是你想要的:

dateadd(day, -datepart(day, Dated), Dated)

However, if you want "regular" subtract one month behavior in tandem with sticking to the end of month, having June 30 fall back to May 31 is slightly trickier. 但是,如果你想要“常规”减少一个月的行为并坚持到月末,那么6月30日回落到5月31日会稍微复杂一些。 There's a discrepancy between the title or your question and the example where it appears you want the final day of month to stay anchored. 标题或问题与您希望月份的最后一天保持稳定的示例之间存在差异。 It would be helpful for you to clarify this. 你有必要澄清这一点。

dateadd(month, -1, ...) doesn't handle that when the previous month has more days than the starting month although it works the other way around. dateadd(month, -1, ...)不会处理上一个月有多天而不是起始月份的情况,尽管它的工作方式相反。 If that's truly what you need I think this should handle it: 如果这真的是你需要的我认为这应该处理它:

case
    when datediff(month, Dated, dateadd(day, 1, Dated)) = 1
    then dateadd(day, -datepart(day, Dated), Dated)
    else dateadd(month, -1, Dated)
end

There's also a flavor of several date functions in that expression and a sense of how this date stuff can get complicated. 在该表达式中还有一些日期函数的含义,以及这种日期函数如何变得复杂的感觉。 The condition in the when looks to see if Dated is the last day of the month by checking that the following day is in a different calendar month. 在该状态when注意查看是否Dated是通过检查次日在不同的日历月份当月的最后一天。 If so we extract the day of month and subtract that many days to jump back to the last day of the previous month. 如果是这样,我们提取月中的日期并减去那么多天以跳回到上个月的最后一天。 (Months start at one not zero. So for example, counting backward 17 days from the 17th lands in the month before.) Otherwise it uses regular dateadd(month, -1, ...) calculations to jump backward to the same day of month. (月份从一开始不是零。所以例如,从前一个月的第17个土地向后计算17天。)否则它使用常规dateadd(month, -1, ...)计算向后跳到同一天月。

Of course if all your dates fall on the end of the month then this simple version will be adequate by itself because it always returns the last day of the previous month (regardless of where it falls in the starting month): 当然,如果您的所有日期都在月末,那么这个简单的版本本身就足够了,因为它总是返回上个月的最后一天(无论它在起始月份的哪个位置):

dateadd(day, -datepart(day, Dated), Dated) /* refer back to the top */
dateadd(day, -day(Dated), Dated) /* same thing */

And just for fun and practice with date expressions, another approach is that you could start on a known month with 31 days and calculate relative to that: 只是为了娱乐和练习日期表达式,另一种方法是你可以在31天的已知月份开始并相对于此计算:

dateadd(month, datediff(month, '20151231', Dated) - 1, '20151231')

This finds the number of months between your date and a reference date. 这会查找您的日期和参考日期之间的月数。 It works for all dates since it doesn't matter whether the difference is positive or negative. 它适用于所有日期,因为差异是正面还是负面并不重要。 So then subtracting one from that difference and adding that many months to the reference point is the result you want. 然后从那个差异中减去一个,并将这几个月添加到参考点就是你想要的结果。

People will come up with some pretty crazy stuff and I'm often amazed (for differing reasons) at some of the variations I see. 人们会想出一些非常疯狂的东西,我常常对我看到的一些变化感到惊讶(出于不同的原因)。 chancrovsky's answer is a good example for closer examination: chancrovsky的答案是仔细检查的一个很好的例子:

dateadd(month, datediff(month, -1, Dated) - 1, -1)

It relies on the fact that date -1 , when treated as implicitly converted to datetime , is the day before January 1, 1900, which does happen to be a month of 31 days as required. 它依赖于以下事实:当被视为隐式转换为datetime时,date -1是1900年1月1日之前的一天,根据需要确实恰好是31天的月份。 (Note that the - 1 in the middle is regular arithmetic and not a date value.) I think most people would advise you to be careful with that one as I'm not sure that it is guaranteed to be portable when Microsoft deprecates features in the future. (请注意,中间的- 1是常规算术而不是日期值。)我想大多数人会建议你小心那个,因为我不确定当微软弃用功能时它是否可以保证。未来。

This also works if you're looking to find dates exactly 6 months behind. 如果您希望找到完全落后6个月的日期,这也有效。

Example: 例:

DATEADD(DAY, DATEDIFF(DAY, -1, dates)-184, -31)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM