简体   繁体   English

如何将nvarchar列转换为MS SQL SERVER 2008 R2中的日期时间类型

[英]How to convert nvarchar column to datetime type in MS SQL SERVER 2008 R2


I have a nvarchar(50) column named "created_time" in MS SQL with the following string format 我在MS SQL中有一个名为“ created_time”的nvarchar(50)列,其字符串格式如下

Thu Mar 03 09:43:25 +0000 2016 2016年3月3日星期四09:43:25 +0000

How do I convert it into a datetime type or select "created_time" as a datetime column? 如何将其转换为日期时间类型或选择“ created_time”作为日期时间列?

Any help would be much appreciated. 任何帮助将非常感激。

Thanks and Regards, 谢谢并恭祝安康,
Christina 克里斯蒂娜

If you ignore the time zone, you can readily use convert() : 如果您忽略时区,则可以轻松地使用convert()

select convert(datetime,
               left(stuff(stuff(created_time, 1, 4, ''), 8, 0, right(created_time, 4) + ' '), 20),
               100)

Actually, the intention here is to convert the string to the format "MMM DD YYYY HH:MI:SS", which is default format so cast() could also be used. 实际上,这里的目的是将字符串转换为“ MMM DD YYYY HH:MI:SS”格式,这是默认格式,因此也可以使用cast()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM