Count Year,Month,Day

  • Comments posted to this topic are about the item Count Year,Month,Day

  • Deny Christian - Thursday, January 19, 2017 12:28 PM

    Comments posted to this topic are about the item Count Year,Month,Day

    There are many ways this article could be improved.
    1. Provide a script with data for the example.
    2. Explain why you believe you need global variables.
    3. Explain why you believe you need a cursor.
    4. Explain how this works in all use cases. (e.g. Leap years, months with different numbers of days)
    5. Provide some analysis of how many reads and writes this query performs during execution. (for bonus points, try 10 items in inventory versus 10,000.
    6. Explain to new coders that you are using the modulus operator.
    7. The singular of Year, Month, and Day versus the plural Years, Months, and Days should be handled.

    Look forward to seeing a redo of this article.

  • i think this can be related to this post:


    COUNTING-TIME-DIFFERENCE-IN-YEAR-MONTH-DAYS

  • You could try the following, substituting in relevant dates in @d1 and @d2:
    declare @d1 datetime = '3-jan-2000'
        ,    @d2 datetime = '2-may-2000'

    select isnull(cast(nullif(case    when datepart(month, @d1) > datepart(month, @d2)
                                    then datediff(year, @d1, @d2) - 1
                                    else datediff(year, @d1, @d2)
                             end, 0) as varchar)
                 + case    when (select case when datepart(month, @d1) > datepart(month, @d2)
                                    then datediff(year, @d1, @d2) - 1
                                    else datediff(year, @d1, @d2)
                             end) > 1 then ' Years, ' else ' Year, ' end, '') +
         isnull(cast(nullif(case when datediff(day, dateadd(month, datediff(month, @d1, @d2), @d1), @d2) < 0
                                    then (datediff(month, @d1, @d2) - 1) % 12
                                    else datediff(month, @d1, @d2) % 12
                             end, 0) as varchar)
                + case when (select case when datediff(day, dateadd(month, datediff(month, @d1, @d2), @d1), @d2) < 0
                                    then (datediff(month, @d1, @d2) - 1) % 12
                                    else datediff(month, @d1, @d2) % 12
                             end) > 1 then ' Months, ' else ' Month, ' end, '') +
         isnull(cast(nullif(case when datediff(day, dateadd(month, datediff(month, @d1, @d2), @d1), @d2) < 0
                                    then datediff(day, dateadd(month, datediff(month, @d1, @d2) -1, @d1), @d2)
                                    else datediff(day, dateadd(month, datediff(month, @d1, @d2), @d1), @d2)
                             end, 0) as varchar)
                + case when (select case when datediff(day, dateadd(month, datediff(month, @d1, @d2), @d1), @d2) < 0
                                    then datediff(day, dateadd(month, datediff(month, @d1, @d2) -1, @d1), @d2)
                                    else datediff(day, dateadd(month, datediff(month, @d1, @d2), @d1), @d2)
                             end) > 1 then ' Days' else ' Day' end, '')

  • Deny Christian - Thursday, January 19, 2017 12:28 PM

    Comments posted to this topic are about the item Count Year,Month,Day

    Did you try this yet? It's on your original thread.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Msg 241, Level 16, State 1, Line 44

    Conversion failed when converting date and/or time from character string.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply