question

red68 avatar image
red68 asked

Age Calculation

I have a DOB field with only a 2-digit year and it calculates correctly unless someone is born before 1950 like 1942 and below. It displays negative numbers. Ex: 11/01/42 DOB is calculating age as "-25". Please help. Thanks!
sql-server-2012
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
What is the data type of the column? Also, what is the 2 year cutoff option set to at the server level? https://msdn.microsoft.com/en-us/library/ms189577.aspx. I'm trying to reproduce and don't have it quite right.
0 Likes 0 ·

1 Answer

·
Oleg avatar image
Oleg answered
This calculation is correct based on the default two digit year cutoff server configuration option. Per [BOL][1], the span is 1950-2049 so 11/01/42 means 11/01/ 2042 while 11/01/50 means 11/01/1950. for example: select cast('11/01/42' as date) DOB; -- returns: DOB ---------- 2042-11-01 Thus, the difference (per datediff) in years between that date and today is -24. While it is possible to change the configuration for this specific option, doing so is ***ill advised*** and should be avoided at any cost. It would be far better to update the offensive values in the underlying table using some logic, like, for example, update the 2-digit year part to 4-digits based on the values (if last 2-digits <= 18 then prefix with 20 else prefix with 19). Here is the sample script: update YourTable set DOB = left(DOB, 6) + case when cast(right(DOB, 2) as int) <= 18 then '20' else '19' end + right(DOB, 2); The above is still not bullet proof because it might incorrectly update the records for people who are more than 100 years old, but it is still better than changing the configuration. Nevertheless, if you really do need to change the default value of 2049, you can use sp\_configure. First, run it as is (without any parameters) and observe whether you see the row with name value of **two digit year cutoff**. If you don't then you need to first enable **show advanced options** by running this: sp_configure 'show advanced options', 1; reconfigure with override; go Once this is done (or advanced options was already enabled), you can now view (by running the procedure without any parameters) or set the **two digit year cutoff** by running this script: exec sp_configure 'two digit year cutoff', 2029; -- or whatever is suitable reconfigure with override; go Again, I am not recommending this approach, just showing that it is possible though admittedly evil. Hope this helps. Oleg [1]: https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
Yup, that's what I headed towards, just wasn't quite there. Excellent explanation.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.