// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Data.Sql; using System.Data.SqlTypes; using System.Data.SqlClient; using Microsoft.SqlServer.Server; namespace E { public class CPR { [SqlFunction] public static SqlDateTime Birthday(string cpr) { int dd = int.Parse(cpr.Substring(0, 2)); int mm = int.Parse(cpr.Substring(2, 2)); int yy = int.Parse(cpr.Substring(4, 2)); int digit = int.Parse(cpr.Substring(6,1)); int yyyy = Century(digit, yy) + yy; if(yyyy >= 1800) { return new DateTime(yyyy, mm, dd); } else { return SqlDateTime.Null; } } private static int Century(int digit, int yy) { switch(digit) { case 0 : case 1 : case 2 : case 3 : return 1900; case 4 : case 9 : if(yy < 37) return 2000; else return 1900; case 5 : case 6 : case 7 : case 8 : if(yy < 37) return 2000; else if(yy > 57) return 1800; else return 0; default : return 0; } } } }