简体   繁体   English

SQL Server:将varchar转换为日期时间

[英]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 我希望将以下varchar(255)数据类型中的datetime转换为datetimedate格式

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 消息241,级别16,状态1,第1行
Conversion failed when converting date and/or time from character string. 从字符串转换日期和/或时间时转换失败。

or alter column gives me the same result. 或alter column给我相同的结果。 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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