[英]converting a varchar to datetime in sql server
I have a varchar 20130909132512
and I would like to convert it into just a date 2013-09-09
我有一个varchar
20130909132512
,我想将其转换成一个日期2013-09-09
I keep getting an error when trying 尝试时我不断收到错误消息
select convert(datetime,'20130909132512',105)
and I am trying to avoid using 而且我试图避免使用
select convert(datetime,SUBSTRING('20130909132512',0,8),105)
if possible. 如果可能的话。 Any ideas on how I can do this?
关于如何执行此操作的任何想法?
Whether you use bummi's STUFF
method or continue using SUBSTRING
, you're going to have to pre-format the string no matter what you do. 无论您使用bummi的
STUFF
方法还是继续使用SUBSTRING
,无论您做什么,都必须预先格式化字符串。
In this case, SUBSTRING
will perform a bit faster 在这种情况下,
SUBSTRING
执行速度会更快
SELECT CONVERT(date, SUBSTRING('20130909132512', 0, 9), 20)
You'll want to use "20" as the conversion style, though, if you want the date in the format 2013-09-09. 但是,如果您希望日期格式为2013-09-09,则需要使用“ 20”作为转换样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.