Sql Server 字符串到日期的转换 [英] Sql Server string to date conversion

查看:65
本文介绍了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.

这篇关于Sql Server 字符串到日期的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆