Hmm, there is already a datediff function which will tell you the difference between two dates. And it can return the values in whichever format you like the difference --> in days, months, years etc.. In addition there is no overhead of converting the date datatype to that of an integer.
https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sqlWhen we comparing two columns , it is best to avoid this overhead of conversion.
The function you have created how would it work when you pass the following parameters
DECLARE @DOB DATE = '19991231';
DECLARE @Today DATE = '20010101';
It tells you there are 18870 integers between 31-Dec-1999 and 1-Jan-2001 and divide by 10000=0.18 which means age is less than a year?
Hi George!
You got the 18870 right. 18870 / 10000 = 1. (1.887 = 1 with integer division.) Your subject is 1 year old.
Overhead: Sure, date to varchar, varchar to integer. There is overhead. If you are running this for 500MM rows, in a single query, you might want write code that is un-readable but performs. But if you are running this one calc at a time - or even 1MM rows at a time - the overhead is minimal and the reliability + readability is king (in my opinion). Benchmark this (I have), you may be surprised....
Thanks!
Curt