2016-03-31 6 views

Antwort

3

Erstellen Sie einfach eine Zeichenfolge in einem geeigneten Format und verwenden Sie CONVERT. Ich bevorzuge Stil 126 (ISO8601): yyyy-mm-ddThh:mi:ss.

-------------------------------------ddmmyyyyhhMMss 
-------------------------------------123456789
DECLARE @charDateTime varchar(50) = '19022016122219'; 
DECLARE @dt datetime; 

-- style 126 (ISO8601) 
-- yyyy-mm-ddThh:mi:ss.mmm (no spaces) 

SET @dt = 
    CONVERT(datetime, 
    SUBSTRING(@charDateTime, 5, 4) + '-' + -- yyyy 
    SUBSTRING(@charDateTime, 3, 2) + '-' + -- mm 
    SUBSTRING(@charDateTime, 1, 2) + 'T' + -- dd 
    SUBSTRING(@charDateTime, 9, 2) + ':' + -- hh 
    SUBSTRING(@charDateTime, 11, 2)+ ':' + -- MM 
    SUBSTRING(@charDateTime, 13, 2)   -- ss 
    ,126); 

SELECT @dt; 

Ergebnis

(No column name) 
2016-02-19 12:22:19.000 
+0

Der war gut. Vielen Dank. – captainsac