简体   繁体   English

MsAccess中的Datediff

[英]Datediff in MsAccess

I am stuck in one place. 我被困在一个地方。 I am using DateDiff in Ms Access it is giving me proper output, like 我在Access女士中使用DateDiff,它给了我适当的输出,例如

StartDate is 10-Sep-2016
EndDate is 15-Oct-2016

Total Days which I will get is 35
& months will i get is 1 Month

DateDiff('d',StartDate,EndDate)

**But I want output as 2 months if it is exeeded the 30 days. **但是如果要超过30天,我希望输出为2个月。 if it is 61 days then 3 months & so on. 如果是61天,则3个月,依此类推。

**IIFFF days diffrence is 
   29 Days then output should be 1 months
   30 Days then output should be 1 months
   32 Days then output should be 2 months
   60 Days then output should be 2 months
   62 Days then output should be 3 months**

Will that be possible in the DateDiff in MsAccess or is there any other function available so that i can achieve the same output.** 在MsAccess的DateDiff中是否可能,或者是否有其他可用功能,以便我可以实现相同的输出。**

You can do this using conditional logic. 您可以使用条件逻辑来做到这一点。 Perhaps something like this: 也许是这样的:

select iif(DateDiff('d', StartDate, EndDate) > 30,
           DateDiff('d',StartDate,EndDate) & " days",
           "2 months"
          )

Your logic that anything exceeding 30 days is "2 months" seems strange. 您认为超过30天为“ 2个月”的逻辑似乎很奇怪。 Normally, I think the logic would look like this: 通常,我认为逻辑将如下所示:

select iif(DateDiff('d', StartDate, EndDate) > 30,
           DateDiff('d', StartDate, EndDate) & " days",
           DateDiff('m', StartDate, EndDate) & " months"
          )

It seems like your minimum count of months for a positive count of days is 1, thus: 看来您对正数天的最小月份数是1,因此:

MonthCount = Sgn(DateDiff("d",StartDate,EndDate)) + DateDiff("m",StartDate,EndDate)

Edit 编辑

For a 30-day cut that will produce your example output, use this simple formula in your query: 对于将产生示例输出的30天切割,请在查询中使用以下简单公式:

MonthCount: (DateDiff("d",[StartDate],[EndDate])-1)\30+1

will this logic suffice to modify your SQL function? 这个逻辑足以修改您的SQL函数吗?

Public Function FN_GET_MONTH(iDays As Long, Optional iDaysInMonth As Long = 30)

    If (iDays / iDaysInMonth) > iDays \ iDaysInMonth Then
        FN_GET_MONTH = (iDays \ iDaysInMonth) + 1
    Else
        FN_GET_MONTH = (iDays \ iDaysInMonth)
    End If

End Function

?FN_GET_MONTH(29) = 1
?FN_GET_MONTH(31) = 2
?FN_GET_MONTH(60) = 2
?FN_GET_MONTH(80) = 3
?FN_GET_MONTH(91) = 4

you can have this public function and use it in your SQL code like 您可以拥有此公共功能,并在您的SQL代码中使用它,例如

FN_GET_MONTH(DateDiff("d", StartDate, EndDate))

This query seems to give the results you seek: 该查询似乎可以提供您想要的结果:

SELECT 
    StartDate,
    EndDate
    numDays,
    ((numDays - 1) \ 30) + 1 AS numMonths
FROM
    (
        SELECT
            StartDate,
            EndDate,
            DateDiff("d", StartDate, EndDate) AS numDays
        FROM YourTable
    )

It gives me 它给我

numDays  numMonths
-------  ---------
...
     29          1
     30          1
     31          2
     32          2
...
     59          2
     60          2
     61          3
     62          3
...

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

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