I'm trying to convert a datetimestamp that is stored as char(20) to datetime.
So, convert a char(20) 202107011008330870 TO a datetime field with this format 2021-07-01 10:14:56.870
I'm trying to convert a datetimestamp that is stored as char(20) to datetime.
So, convert a char(20) 202107011008330870 TO a datetime field with this format 2021-07-01 10:14:56.870
One method would be to inject the necessary characters into the string, and then CONVERT
:
SELECT TRY_CONVERT(datetime2(4),STUFF(STUFF(STUFF(STUFF(V.YourString,15,0,'.'),13,0,':'),11,0,':'),9,0,' '))
FROM (VALUES('202107011008330870'))V(YourString);