I have a table with a varchar column which represent date and time in the next format:
dd/MM/yy hh:mm
For example: 27/01/13 07:57
I need to convert this column to DateTime type.
I tried to do it without success.
Assume that @varcharDT contains the VarChar DateTime and I want insert the converted value to @dateTimeDT variable.
DECLARE @varcharDT varchar(1000)
SELECT @varcharDT = dateTimeColumn from dbo.tempTable
--Trying to convert
DECLARE @dateTimeDT DateTime
SET @dateTimeDT= CONVERT (DATETIME, @varcharDT )
The last line raise the next exception:
Conversion failed when converting date and/or time from character string.
I think the conversion fails because my VarChar DateTime is in custom format.
How can I solve it?
I am working with SQL Server 2008 R2
Thanks
Try:
The last number is the style for the convert. The list can be found in the MSDN documentation article CAST and CONVERT (Transact-SQL).