Mysql 如何将其转换为DATETIME SQL?

Mysql 如何将其转换为DATETIME SQL?,mysql,sql,string,datetime,Mysql,Sql,String,Datetime,正在尝试将此值转换为DATETIME,但迄今为止未成功。思想和想法是受欢迎的 03MAR2020:02:45:58.977000 在SQL Server中 03MAR2020:02:45:58.977000无法转换totdatetime,因为首先在年份之后有一个额外的:符号,小数秒是6位,而不是最大3位 如果字符串的长度和格式始终相同,则可以使用以下方法解决此问题: DECLARE @example varchar(25) = '03MAR2020:02:45:58.977000' SET @

正在尝试将此值转换为
DATETIME
,但迄今为止未成功。思想和想法是受欢迎的

03MAR2020:02:45:58.977000
在SQL Server中

03MAR2020:02:45:58.977000无法转换tot
datetime
,因为首先在年份之后有一个额外的
符号,小数秒是6位,而不是最大3位

如果字符串的长度和格式始终相同,则可以使用以下方法解决此问题:

DECLARE @example varchar(25) = '03MAR2020:02:45:58.977000'
SET @example = LEFT(@example, 22)   --- remove last 3 zeros
SET @example = STUFF(@example, CHARINDEX(':', @example), LEN(':'), ' ')  ---repalce the first ':'
SELECT convert(datetime, @example) --- convert to datetime
更多详细信息:以下是SQL Server中数据类型的
datetime
部分

YYYY is four digits from 1753 through 9999 that represent a year.

MM is two digits, ranging from 01 to 12, that represent a month in the specified year.

DD is two digits, ranging from 01 to 31 depending on the month, that represent a day of the specified month.

hh is two digits, ranging from 00 to 23, that represent the hour.

mm is two digits, ranging from 00 to 59, that represent the minute.

ss is two digits, ranging from 00 to 59, that represent the second.

n* is zero to three digits, ranging from 0 to 999, that represent the fractional seconds.
摘自SQL server文档

您可以使用适当格式的功能:

select str_to_date('03MAR2020:02:45:58.977000', '%d%b%Y:%k:%i:%s.%f')
请参阅。
结果:


日期函数是高度特定于数据库的。请用您正在使用的数据库标记您的问题:myql、oracle、postgresql…?抱歉,我们正在起诉MySQL,因为我们意识到问题是针对MySQL的。我回答了SQL Server的问题。我把它留在这里,以防对别人有帮助
2020-03-03 02:45:58.977000