Convert Varchar(8) to DateTime

  • I have a stored proc that accepts input parameters from an asp page. The value entered for an issueDate textbox on the page is a in the format of 'yyyymmdd''. This value is passed to the stored proc as VarChar(8), but I need to convert the value of this input parameter to datetime to insert it into a table. When I use:

    Convert(DateTime, @issueDate, 112),

    I get

    1900 00 00 00:00 inserted into the table.

    What is the solution?

    Thank you

  • Try this

    SELECT CAST(@issueDate AS DATETIME)


    Bru Medishetty

    Blog -- LearnSQLWithBru

    Join on Facebook Page Facebook.comLearnSQLWithBru

    Twitter -- BruMedishetty

  • I don't get the same problem. When that date pops up, it could be due to an invalid date that has been input.

    declare @issuedate varchar(8)

    Set @issuedate = '20091118'

    select Convert(DateTime, @issueDate, 112),CAST(@issueDate AS DATETIME)

    I tested with both cast and convert to verify and both return the same value. Even If I change the date to '091118' I still get the correct date.

    Here is an article with some date format info. Maybe a locale is set differently than expected.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thanks for the prompt reply. I will test the Cast, Convert function did not work.

  • Thanks. I will try it Convert function did not work.

  • Actually Cast(@issuedate As DateTime) worked.

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

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