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

  • You have some serious problems and the person responsible for dropping the century part of the date should be taken down a dark ally. (I've been reading too many of Celkos posts, :-D)

    As they really should have learnt from the Y2K issues that fixed the data when it was ported from what ever system used a 6 byte field for dates.

    However, thats what you have to work with, and its now a proble. Is the RSAId a varchar if Int?

    Its its an Int then The first thing you need to do is convert them to character strings (varchar(6)), and pad with leading zeros., if they are already character strings then you should be ok.

    This sample code will work for dates between 1950-2049 anything outside this range, say 1940 needs more work, see the date 490101

    Declare @dates Table (dob varchar(6))

    Insert into @dates

    values ('000101'),('490101'),('800101')

    Select

    convert(Date,dob))

    From @dates

    For dates before 1950 you are going to be in a world of hurt deciding if 100101 is 01-Jan-2010 or 01-Jan-1910.

    heres an example of some logic that will help but it wont work for anyone born 1900-1912

    Declare @dates Table (dob varchar(6))

    Insert into @dates

    values ('000101'),('490101'),('800101'),('100101')

    Select

    convert(Date, Case When cast(Left(dob,2) as tinyInt)<50

    and cast(Left(dob,2) as tinyInt)>right(cast(Year(getdate()) as varchar(4)),2) then '19'

    Else '20' End + dob)

    from @dates

    IF you have an Age on the table it can be used to make an assupmtion, but its very much a problem.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices