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

  • Dixie's solution seems simple, but won't work for dates before 1950.

    I've done this that would work for any date between today and 100 years in the past (almost).

    And a simple test.

    DECLARE @tDatestable(

    RSAIDchar(8));

    WITH E1(N) AS (

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1

    ),

    E2(N) AS (SELECT 1 FROM E1 a, E1 b)

    INSERT @tDates

    SELECT RIGHT( '00'+ CAST(ROW_NUMBER() OVER(ORDER BY N) - 1 AS varchar(2)), 2) + '0101'

    FROM E2

    SELECT RSAID,

    CASE WHEN convert(date, RSAID) <= GETDATE() --LEFT( RSAID, 2) <= RIGHT( YEAR(GETDATE()),2)

    THEN convert(char(11),convert(date,'20' + RSAID),106)

    ELSE convert(char(11),convert(date,'19' + RSAID),106) END

    FROM @tDates

    ORDER BY 2

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2