【问题标题】:Datediff in MsAccess访问女士中的日期差异
【发布时间】:2017-01-26 06:29:52
【问题描述】:

我被困在一个地方。 我在 Ms 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)

**但是如果超过 30 天,我希望输出为 2 个月。 如果是 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**

这在 MsAccess 的 DateDiff 中是否可行 或者是否有任何其他可用的功能,以便我可以实现相同的输出。**

【问题讨论】:

    标签: ms-access ms-access-2010 ms-access-2007 datediff


    【解决方案1】:

    您可以使用条件逻辑来做到这一点。也许是这样的:

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

    您认为超过 30 天的任何时间都是“2 个月”的逻辑似乎很奇怪。通常,我认为逻辑应该是这样的:

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

    【讨论】:

    • 先生,其实我不是每次都想要2个月,如果是61天那么我想要3个月等等
    • @HardikParmar 。 . .因此,答案的第二部分。您可能需要在月份数上加 1。
    【解决方案2】:

    这个逻辑是否足以修改您的 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
    

    您可以拥有这个公共函数并在您的 SQL 代码中使用它,例如

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

    【讨论】:

    • 你的答案对我来说是完美的匹配。您的理解与我的非常相似,但我使用的是 MS ACCESS 数据库。
    • Ms access..你可以在access SQL中使用vba公共函数。 @hardik parmar
    【解决方案3】:

    这个查询似乎给出了你想要的结果:

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

    它给了我

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

    【讨论】:

      【解决方案4】:

      您的正数天数的最小月数似乎是 1,因此:

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

      编辑

      对于将产生示例输出的 30 天切割,请在查询中使用以下简单公式:

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

      【讨论】:

        猜你喜欢
        • 2013-10-05
        • 1970-01-01
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2012-08-29
        相关资源
        最近更新 更多