Home Forums SQL Server 2008 T-SQL (SS2K8) Formatting date of birth using South African ID number RE: Formatting date of birth using South African ID number

  • The Dixie Flatline (11/9/2012)


    There's a function for what you want: CONVERT()

    select convert(varchar(6),convert(date,'01 Jan 1980'),12)

    select convert(varchar(6),convert(date,'01 Jan 2000'),12)

    The inner CONVERT function turns your string into a DATE character type. The outer string converts it back to a string. The "12" parm at the end says display the date in YYMMDD format. Other formats are available in MSDN Books Online (BOL).

    The original problem is that the date is stored as YYMMDD, so how would you cast 120101 as a DOB, is it 01-Jan-2012 or 01-Jan-1900, SQL server will assume its 2012, due to the server settings as it falls in the range 1950-2049.

    However its feasible that a person born pre 1950 is still alive, you can therefore assume anything in the range current year to 2049 is 1900, but its also feasible that people are over 99 years old. so you may actually assign the wrong century to the persons Dob.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices