A very common requirement is to calculate aging. With the help of below SQL script, we will calculate the age very easily.
DECLARE @MTH INT,
@Years INT,
@Months INT,
@Dates INT,
@GETDays INT
DECLARE @STARTDATE AS DATETIME,
@ENDDATE AS DATETIME
SET @STARTDATE = format(cast('03/01/1988' as date),'yyyyMMdd')
SET @ENDDATE = GETDATE()
SET @Years = DATEDIFF(YY, @STARTDATE, @ENDDATE)
SET @Months = DATEDIFF(MM, @STARTDATE, @ENDDATE)
SET @MTH = CASE
WHEN DATEPART(DD, @ENDDATE) <= DATEPART(DD, @STARTDATE)
THEN ABS(DATEDIFF(MM, DATEADD(YY, @Years, @STARTDATE), @ENDDATE)) - 1
ELSE ABS(DATEDIFF(MM, DATEADD(YY, @Years, @STARTDATE), @ENDDATE))
END
SET @GETDays = CASE
WHEN DATEPART(DD, @ENDDATE) <= DATEPART(DD, @STARTDATE)
THEN (DATEPART(DD, @ENDDATE) + DAY(EOMONTH(DATEADD(MM, - 1, GETDATE())))) - DATEPART(DD, @STARTDATE)
ELSE DATEPART(DD, @ENDDATE) - DATEPART(DD, @STARTDATE)
END
SELECT CONVERT(VARCHAR(10), @Years) + ' YEAR(s) ' + CONVERT(VARCHAR(10), @MTH) + ' MONTH(s) ' + CONVERT(VARCHAR(10), @GETDays) + ' DAY(s)' Age
|
The input we need to give as MM/DD/YYYY format.
Here I am calculating my age. My Birth date is 03/01/1988. See the output.
Nice information on MSBI Online Training
ReplyDeleteYou are shared valuable information with us, It's very useful for learners. Thanks for sharing info about MSBI Online Training Hyderabad
ReplyDeleteYou are rocking Bagesh.
ReplyDeleteAppreciate Your work, Very Informative Post on MSBI. I Would like to Share Something about MSBI Training. I think it will be beneficial for the Users here.
ReplyDeleteAppreciate Your work, Very Informative Post on MSBI. I Would like to Share Something about MSBI Training.I think it will be beneficial for the Users here.
ReplyDeleteI really enjoy the blog.Much thanks again. Really Great MSBI online course Bangalore
ReplyDeleteIt is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful.MSBI Training Institutes in Bangalore
ReplyDeleteThrough this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing on Msbi online training
ReplyDeleteMsbi online training Hyderabad
Msbi online training India
Msbi online course
Msbi course
Msbi training
Msbi certification training
P0wer bi onlinetraining
ReplyDeleteP0wer bi training
nice post.aws training
ReplyDeleteaws online training
aws online course
office 365 training
office 365 online training
office 365 online course
nice information thanks for sharing this article very useful for me ................!
ReplyDeletemsbi course training
nice information .....!
ReplyDeletemsbi course training
Thanks for sharing useful information..
ReplyDeleteMulesoft Training
Mulesoft Online Training