Search code examples
sql-servert-sqldatetimesql-server-2005string-to-datetime

Sql Server string to date conversion


I want to convert a string like this:

'10/15/2008 10:06:32 PM'

into the equivalent DATETIME value in Sql Server.

In Oracle, I would say this:

TO_DATE('10/15/2008 10:06:32 PM','MM/DD/YYYY HH:MI:SS AM')

This question implies that I must parse the string into one of the standard formats, and then convert using one of those codes. That seems ludicrous for such a mundane operation. Is there an easier way?


Solution

  • Try this

    Cast('7/7/2011' as datetime)
    

    and

    Convert(DATETIME, '7/7/2011', 101)
    

    See CAST and CONVERT (Transact-SQL) for more details.