• DBASkippack (8/17/2009)


    Is it posible to convert the following using T-SQL:

    MMDDYY to YYYYMMDD

    For example, 081709 needs to be rendered as 20090817

    thanks in advance..

    Well, I'm not sure if there's a CAST or CONVERT statement that would work for you, but what you could do is something like,

    DECLARE @InputDate VARCHAR(8)

    SET @InputDate = '081709'

    DECLARE @InputYear VARCHAR(4)

    DECLARE @InputMonth VARCHAR(2)

    DECLARE @InputDay VARCHAR(2)

    SET @InputYear = '20' + SUBSTRING(@InputDate, 5, 2)

    SET @InputMonth = LEFT(@InputDate, 2)

    SET @InputDay = SUBSTRING(@InputDate, 3, 2)

    DECLARE @OutputDate SMALLDATETIME

    SET @OutputDate = CAST(@InputYear + @InputMonth + @InputDay AS SMALLDATETIME)

    PRINT @OutputDate

    Course, that's reliant upon this being in the year 2000. If your dates can fall between any number of centuries, then you'll need to use some logic to change the '20' part.