简体   繁体   English

哪种方法与DATEADD()比较准确?

[英]Which method is more accurate with DATEADD()?

Following are two ways of adding days and months to a given date. 以下是将天和月添加到给定日期的两种方法。 Both seem to be logically correct but returns different values. 两者似乎在逻辑上都是正确的,但是返回不同的值。

Column number 1: Add months and then days, Column number 2: Add days and then months 第1列:先添加月份,然后是天,第2列:先添加日期,然后是几个月

DECLARE @d DATE = '20140128'

SELECT  DATEADD(DAY, 3, DATEADD(MONTH, 1, @d)) Add_Months_Days,  
        DATEADD(MONTH, 1, DATEADD(DAY, 3, @d)) Add_Days_Months

Results and fiddle 结果和小提琴

Add_Months_Days        Add_Days_Months
----------------       ----------------
2014-03-03             2014-02-28

I understand why it is happening and both are logical too. 我了解为什么会发生,而且两者都是合乎逻辑的。 But in a situation where we need to add months and days to a given date at the same time, is there a standard way to do this? 但是在一种情况下,我们需要在一个给定的日期上同时增加数月和数天,是否有一种标准方法可以做到这一点?

I believe they are both correct, but they do different things. 我相信它们都是正确的,但是它们做的事情不同。

MSDN states : MSDN指出

If datepart is month and the date month has more days than the return month and the date day does not exist in the return month, the last day of the return month is returned. 如果datepart是month并且日期month的日期比返回月份的天数更多,并且date day在返回月份中不存在,则返回返回月份的最后一天。

In the first example you first add 1 month to 20140128 making it 20140228 (a valid date) and then add 3 days, ending up with 20140303 . 在第一个示例中,您首先向20140128添加1个月,使其成为20140228 (有效日期),然后添加3天,最后以20140303结束。

In the second example however you add 3 days, getting 20140131 and then add 1 month, but since February 2014 only has 28 days you'll get 20140228 as the result as the dateadd operation returns the last day of the month as stated above. 但是,在第二个示例中,您添加了3天,获得20140131 ,然后添加1个月,但是由于2014年2月只有28天,因此结果为20140228 ,因为dateadd操作如上所述返回了月份的最后一天。

I don't believe there is a standard way of doing this, I would think it comes down to the specific business requirements, however I personally think doing month-day and getting the latter end date might be "more correct" as it seem to follow from the intent (the day-month method seem to lose a few days). 我不认为有这样做的标准方法,我认为这取决于具体的业务要求,但是我个人认为,按月进行工作并获得后者的结束日期似乎更“正确”。从意图上遵循(按月的方法似乎要浪费几天)。

They are both logical but return different results as implicit in your question is the truncation of the add-month result to month-end, should it take you over a month-boundary. 它们都是合乎逻辑的,但是返回不同的结果,因为您的问题中隐含的是将添加月份的结果截断为月末,如果您需要跨月限制的话。 You have this in the second query, but not the first. 您在第二个查询中有此查询,但没有第一个查询。

Adding MONTH (1) to a date ("20140128"), will not add total days of the month (Jan - 31, Feb - 28 or 29, etc.) . 将MONTH(1)添加到日期(“ 20140128”), 将不会添加该月的总天数(1月31日,2月28日或29等) It will add the given MONTH value (1) to the input date and result will be "20140228". 它将给定的MONTH值(1)添加到输入日期,结果将为“ 20140228”。

Please refer this Question and Answer 请参考此问答

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

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