Sql Server 字符串到日期的转换

我想转换这样的字符串:

I want to convert a string like this:

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

转换为 SQL Server 中等效的 DATETIME 值.

into the equivalent DATETIME value in Sql Server.

在 Oracle 中,我会这样说:

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?

推荐答案

试试这个

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

Convert(varchar(30),'7/7/2011',102)

参见 CAST 和 CONVERT (Transact-SQL) 了解更多详情.

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

相关文章