Convert 6 character varchar to datetime

  • How can I convert a 6 character string (varchar) 062006 to a datetime?

  • is it 06/20/2006 or 06/01/2006?

    One of those should work. They are primitive but as long as you can goarantee the input format to be always 6 chars then you should be fine

    DECLARE @StringDate CHAR(6)

    SET @StringDate = '062006'

    DECLARE @Date DATETIME

    SELECT CONVERT(DATETIME, LEFT(@StringDate,2) + '/' + SUBSTRING(@StringDate,3,2) + '/' + RIGHT(@StringDate,2), 1)

    DECLARE @StringDate CHAR(6)

    SET @StringDate = '062006'

    DECLARE @Date DATETIME

    SELECT CONVERT(DATETIME, LEFT(@StringDate,2) + '/01/' + RIGHT(@StringDate,4), 101)

    ---------------------------------------------
    [font="Verdana"]Nothing is impossible.
    It is just a matter of time and money.[/font]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply