Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the duration between two dates

I want to find the duration between the two date columns. For this i used the DATEDIFF function to find number years and months separately but wanted both results in single column. The two columns are given below.

start_dt      |    end_dt
06-Oct-2009      15-Jul-2011  

Result which needed

Duration(years.months)
2.3
like image 382
sudhakarssd Avatar asked Oct 26 '25 02:10

sudhakarssd


1 Answers

I think there is no out-of-the-box API to provide the result in the format you mentioned. You need to use the DATEDIFF function to get the difference in the least denomination you need and then divide the result with appropriate value to get the duration in the format required. Something like this:

DECLARE @start DATETIME
DECLARE @end DATETIME
DECLARE @duration INT

SELECT @start = '2009-10-06', @end = '2011-07-15'
SELECT @duration = DATEDIFF(mm, @start, @end)
SELECT CONVERT(NVARCHAR, @duration / 12) + '.' + CONVERT(NVARCHAR, @duration % 12)

This can be better achieved by writing a function that would take the dates and least denomination and returns the duration in the format needed, as it would require TSQL and plain SQL wouldn't suffice.

like image 78
Vikdor Avatar answered Oct 27 '25 18:10

Vikdor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!