Monday, September 12, 2016

Sql server: Convert total number of days into month(s) and day(s)

In this article I am going to explain how to get the number of days between two dates and convert days into month(s) and day(s).

Sql server: Convert total number of days into month(s) and day(s)



Implementation:
I want to convert total number of days into months and days between 2 dates. Here is the query that will work:

SET DATEFORMAT dmy;

declare @start date ='01/07/2016'
declare @end date ='03/09/2016'

select [MONTH and DAY]=(cast(DATEDIFF(MM,@start,@end) as varchar)+' Month(s) '
+ cast(DATEDIFF(DD, @start, DATEADD(MM, - DATEDIFF(MM, @start, @end),
            @end)) AS varchar) + ' Day(s) ')
  



No comments:

Post a Comment