Efficient Age From Date of Birth Calculation
I have been playing around with MS SQL and I was wondering what would be the most efficient method to gather a users age. Calculations involving dates should always be considered from their most tricky state, if you consider that you might need to take into account UTC time, leap years, time zones it all starts to add up to complexity that you can too easily trip up over. So my first thought when someone asks me to calculate the age from a DOB field is to "cheat" and use the built in languages tools. Most languages have some form of datediff function that can be used to calculate the time between two dates. However is this going to be the most efficient method? I thought I would test a couple of methods on MS SQL to see the performance changes. All tests were performed on a low spec laptop on MS SQL Express on a set of 100 random date of births First method SELECT floor(datediff(day,[dob],current_timestamp) / 365.25) Well this is nice and succinct but does ...