SQL Server 2008 - 将 varchar 转换为日期时间列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16561856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Server 2008 - convert varchar to datetime column
提问by user2119912
I have a table with a varchar
column (dateandtime
) with date and time in the format dd/mm/yyyy hh:mm:ss
and I need to convert it to a datetime
column.
我有一个带有日期和时间格式的varchar
列 ( dateandtime
)的表格,dd/mm/yyyy hh:mm:ss
我需要将其转换为一datetime
列。
I have a temp column dateandtimetemp
in the table .
我dateandtimetemp
在表中有一个临时列。
Thanks
谢谢
回答by Semih Yagcioglu
Try this:
尝试这个:
SELECT CONVERT(Datetime, '15/05/2013 13:55:12', 104)
It should return : 2013-05-15 13:55:12.000
它应该返回: 2013-05-15 13:55:12.000
回答by Abhishek B Patel
Try
尝试
SELECT CONVERT(VARCHAR(30),GETDATE(),113) ;
it return result in following format
它以以下格式返回结果
15 May 2013 16:26:29:850
2013 年 5 月 15 日 16:26:29:850
回答by marc_s
So then just go ahead and try it!
那么就继续尝试吧!
UPDATE dbo.YourTable
SET DateAndTimeTemp = CAST(DateAndTime AS DATETIME)
and see if it works. If your input data is really always properly defined - you should have no issues here.
看看它是否有效。如果您的输入数据确实始终正确定义 - 您应该没有问题。
This of course depends on what langauge/dateformat setting you have activated in your database - so that might be the first problem you encounter.
这当然取决于您在数据库中激活的语言/日期格式设置 - 所以这可能是您遇到的第一个问题。
If you do have issues, then you can always "clean up" your input data and try again ...
如果您确实有问题,那么您可以随时“清理”您的输入数据并重试......
回答by heikkim
Try this
尝试这个
SELECT CONVERT(DATETIME, '15/05/2013 11:12:13', 103)