Sql Server string to date conversion(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 字符串到日期的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sql Server 字符串到日期的转换


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01