SQL Server varchar to datetime

Posted by Dezigo on Stack Overflow See other posts from Stack Overflow or by Dezigo
Published on 2010-05-21T08:44:06Z Indexed on 2010/05/21 9:00 UTC
Read the original article Hit count: 142

Filed under:

I have a field varchar(14) = 20090226115644

I need convert it to -> 2009-02-26 11:56:44 (datetime format)

My idea. use cast and convert.. but I always have errors.

Conversion failed when converting datetime from character string.

I made this, but don`t like it..

SELECT  
    SUBSTRING(move,1,4) + '-' + SUBSTRING(move,5,2) + '-' + SUBSTRING(move,7,2) + ' ' + SUBSTRING(move,9,2) + ':' + SUBSTRING(move,11,2) + ':'+SUBSTRING(move,13,2) as new -- 
FROM [Test].[dbo].[container_events]
where move IS not null

Result :2009-02-26 11:56:44

© Stack Overflow or respective owner

Related posts about sql-server