tsql无法将字符串转换为datetime

时间:2014-04-11 07:55:40

标签: string tsql datetime

当我从文本字段构建日期时,我收到错误。    "服务器:消息241,级别16,状态1,行1      从字符串转换datetime的语法错误。" 我使用的文字就像 "航班提醒:尊敬的客户,2014年2月28日/ 28日航班延误,新ETD为0530小时/ 29Mar。"

这是我的查询

    SELECT 
--- Extracting Flight No from Message field --
case
 when substring(oh_message,(charindex('/',OH_MESSAGE)-4),1) = '-' 
   or substring(oh_message,(charindex('/',OH_MESSAGE)-4),1) = 'K'
 THEN
   substring(oh_message,(charindex('/',OH_MESSAGE)-3),3)
 else
   substring(oh_message,(charindex('/',OH_MESSAGE)-4),4)
END AS flightno,
---- next Flight Date
substring(oh_message,(CHARINDEX('/',Oh_message)) + 1
,5)as DtMnth,

---- next Flight Year
substring(oh_message,(CHARINDEX('/',Oh_message)) + 7
,4)as DtYr,
-- Flight Hours
case
when source = 'SNMv2' THEN(
substring(oh_message,(CHARINDEX('is on time at',Oh_message)) + 14,4))
else
substring(oh_message,(CHARINDEX('new ETD is',Oh_message)) + 11,4)
end as fhour,

--- Converting to datetime format
CONVERT(datetime,
 (substring(oh_message,(CHARINDEX('/',Oh_message)) + 3,3))
  + ' ' + 
 (substring(oh_message,(CHARINDEX('/',Oh_message)) + 1,2)) 
 + ' ' + 
 (substring(oh_message,(CHARINDEX('/',Oh_message)) + 7,4))
,109)

,OH_MESSAGE


 from snmnsnmv2view    

0 个答案:

没有答案