I have a column of dates in varchar and i need to convert to date

旧城冷巷雨未停 提交于 2019-12-13 18:41:40

问题


The dates are currently displayed as: ddmmmyyyy (12DEC2013)

I've been playing around with this formula:

DECLARE @Date char(8)
set @Date='12312009'
SELECT CONVERT(datetime,RIGHT(@Date,4)+LEFT(@Date,2)+SUBSTRING(@Date,3,2))

but I didn't have any success, can someone help me out with this. Additionally all my dates are in a column called TERMDT and I'd like to put all the new date values in a new column formatted as such.


回答1:


Just give convert() an appropriate 3rd argument (the format):

SELECT CONVERT(datetime,
               RIGHT(d, 4) + LEFT(d,2) + SUBSTRING(d, 3, 2),
               112)
from (select '12312009' as d) t


来源:https://stackoverflow.com/questions/25727125/i-have-a-column-of-dates-in-varchar-and-i-need-to-convert-to-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!