## Count Year,Month,Day

 Author Message Deny Christian SSC Veteran Group: General Forum Members Points: 211 Visits: 163 Comments posted to this topic are about the item Count Year,Month,Day John R. Hanson Valued Member Group: General Forum Members Points: 69 Visits: 122 +xDeny Christian - Thursday, January 19, 2017 12:28 PMComments posted to this topic are about the item Count Year,Month,DayThere 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. paulo.margarido SSC Veteran Group: General Forum Members Points: 234 Visits: 142 i think this can be related to this post:COUNTING-TIME-DIFFERENCE-IN-YEAR-MONTH-DAYS JohnCurrie SSC-Enthusiastic Group: General Forum Members Points: 194 Visits: 373 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, '')` ChrisM@Work SSChampion Group: General Forum Members Points: 13844 Visits: 19436 +xDeny Christian - Thursday, January 19, 2017 12:28 PMComments posted to this topic are about the item Count Year,Month,DayDid 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 ShawFor 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 ModenExploring Recursive CTEs by Example Dwain Camps ChrisM@Work SSChampion Group: General Forum Members Points: 13844 Visits: 19436 Msg 241, Level 16, State 1, Line 44Conversion 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 ShawFor 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 ModenExploring Recursive CTEs by Example Dwain Camps