Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count Year,Month,Day


Count Year,Month,Day

Author
Message
Deny Christian
Deny Christian
SSC Veteran
SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)SSC Veteran (211 reputation)

Group: General Forum Members
Points: 211 Visits: 163
Comments posted to this topic are about the item Count Year,Month,Day
John R. Hanson
John R. Hanson
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 122
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.

paulo.margarido
paulo.margarido
SSC Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

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
JohnCurrie
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

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
ChrisM@Work
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13844 Visits: 19436
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
Exploring Recursive CTEs by Example Dwain Camps
ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13844 Visits: 19436

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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search