DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days
int
SELECT @date = '09/03/1985'
--date in mm/dd/yy
SELECT @tmpdate = @date
SELECT
@years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN MONTH(@date) >
MONTH(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years,
@tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN
DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m,
@months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate,
GETDATE())
SELECT @years AS Years, @months AS Months, @days as
Days
No comments:
Post a Comment