Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Why am I only getting the years ?

$
0
0

Hi guys,  I'm executing the following code but my result is only returning 26 years and not the months and days too, any ideas what am I doing wrong? would appreciate the help, thanks :-) 

DECLARE @DOB datetime, @TempDate datetime, @Years int, @Months int, @Days int

SET @DOB = '09/17/1987'

SELECT @TempDate = @DOB

SELECT @Years = DATEDIFF(YEAR, @TempDate, GETDATE()) -
              CASE
			      WHEN (MONTH(@DOB) > MONTH(GETDATE())) OR
				  (MONTH(@DOB) = MONTH(GETDATE()) AND DAY(@DOB) > DAY(GETDATE()))
				  THEN 1 ELSE 0
				  END

SELECT @TempDate = DATEADD(YEAR, @Years, @TempDate)

SELECT @Months = DATEDIFF(MONTH, @Months, @TempDate) -
                CASE 
				     WHEN DAY(@DOB) > DAY(GETDATE())
					 THEN 1 ELSE 0
					 END

SELECT @TempDate = DATEADD(MONTH, @Months, @TempDate)

SELECT @Days = DATEDIFF(DAY, @Days, @TempDate)


SELECT @Years AS [Years], @Months AS [Months], @Days AS [Days];


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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