Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Convert varchar(14) to datetime

Author  Topic 

joriveek
Starting Member

33 Posts

Posted - 2007-04-02 : 06:39:00
Hi

I am getting the date in the form of varchar(14) like say

x = '20040102102425'

it is like YYYYMMDDHHMMSS

Now I want to convert this string as normal datetime and store into SQL table. I think it is DD/MM/YYYY HH:MM:SS

Is there any way I can do this SQL Server 2005?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-02 : 06:44:50
[code]DECLARE @x VARCHAR(14)

SET @x = '20040102102425'

SELECT @x,
STUFF(STUFF(STUFF(@x, 9, 0, ' '), 12, 0, ':'), 15, 0, ':'),
CAST(STUFF(STUFF(STUFF(@x, 9, 0, ' '), 12, 0, ':'), 15, 0, ':') AS DATETIME)[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -