Sql Server 字符串到日期的转换

2023-07-17数据库问题
0

本文介绍了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 字符串到日期的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14

sql group by 与不同
sql group by versus distinct(sql group by 与不同)...
2024-04-16 数据库问题
37

如何在SQL中返回每个组的增量组号
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)...
2024-04-16 数据库问题
8

统计分组返回的记录数
Count number of records returned by group by(统计分组返回的记录数)...
2024-04-16 数据库问题
10