Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

date difference

$
0
0

the below returns

3 Year -5 Days 0 Months =1086 Days Continued service

but the expected output is 3 years 0 months and 12 days 

please help

DECLARE @StartDate    datetime
DECLARE @EndDate      datetime
DECLARE @Year           int
DECLARE @Month          int
DECLARE @Day            int
DECLARE @DCS          int
DECLARE @7GTime     int

SET @StartDate = '20100117'
SET @EndDate = '20130117'


SELECT @EndDate
SELECT @EndDate = DATEADD (day , -5 , @EndDate)
SELECT @EndDate

SELECT @Year=DATEDIFF(YEAR,@StartDate,@EndDate)-- To find Years
SELECT @Month =datediff(MONTH,@StartDate,@EndDate)-(datediff(year,@StartDate,@EndDate)*12)--To find the months
SELECT @Day=DATEPART(d,@EndDate)-datepart(d,@StartDate)-- To Find Days
SELECT @DCS = (datediff(day, @StartDate,@EndDate)) - 5
SELECT @Day

SELECT  CONVERT(varchar(10),@Year) + ' Year '  + CONVERT(varchar(10),@Day) + ' Days ' + CONVERT(varchar(10),@Month) + ' Months =' +CONVERT(varchar(10), @DCS) + ' Days Continued service'



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>