Convert char to datetime

  • I am loading Perfmon data into a SQL table, and unfortunately the CounterDateTime comes in as a char data type.  I need to query this data based on time ranges, but am getting the error "Conversion failed when converting character string to datetime data type" when I try to use cast or convert.  Is there any way to do this??  Thanks for any input...

  • Can you provide some sample data that you have? It should be an implicit conversion from char to datetime. See the following link: https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

    The chart about 1/3 of the way down shows implicit conversion; meaning you shouldn't have to Cast or Convert (but you can if you're having problems with it).

    Alan H
    MCSE - Data Management and Analytics
    Senior SQL Server DBA

    Best way to ask a question: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Datetimes are char data when inputting to SQL Server.  As long as the value is valid, don't specify your own CAST or CONVERT, let SQL do it implicitly.

    If you have invalid data in the column, that is a true problem, of course.  You would need to address that, typically by checking the data for validity as you are loading.  I can't give any more details on that process, since you didn't specify how you were loading the data to SQL.

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

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

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