How do I to convert varchar datatype to datetime

  • Does anyone know how to convert varchar datatype to datetime in TSQL?

  • CAST and CONVERT (Transact-SQL)

    Is there any problem with that?


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow

  • leonie6214 (8/5/2012)


    Does anyone know how to convert varchar datatype to datetime in TSQL?

    please give example of what you want to convert

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • As previously stated you can use CAST or CONVERT. But like many items in T-SQL "It depends". As an example ... be aware of your system date format, and of course the format of the actual data.

    DECLARE @T VARCHAR(40)

    SET DATEFORMAT dmy

    SET @T = '2012-08-05 09:13:50.053'

    SELECT CAST(@T AS DATETIME)

    SET @T = '20120805 09:13:50.053'

    SELECT CAST(@T AS DATETIME)

    Results:

    2012-05-08 09:13:50.053

    2012-08-05 09:13:50.053

    As is obvious the results are dramatically different. If you would post a sample of your actual data, and your systems dateformat someone may be able to assist you furter.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

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