简体   繁体   中英

SQL Server: Convert a varchar to a datetime

I would like a date in a varchar(255) data type as below to be converted to a datetime or date format

05 jun 2007  

When I use a cast with either the datetime or date:

UPDATE [dbo].[SSIS_TempDump_Episode_Numbers] 
SET DischargeDate = CAST([Discharge date] as Date)

I get this error:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

or alter column gives me the same result. is it because of the date format?

DECLARE @foo VARCHAR(255)

SET @foo = '05 jun 2007'

PRINT CONVERT(DATETIME,@foo)
Select CONVERT(datetime,'05 jun 2007') -->'2007-06-05 00:00:00.000'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM