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

2019-08-08 19:20发布

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条回答
forever°为你锁心
2楼-- · 2019-08-08 19:59

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
查看更多
登录 后发表回答