日期/时间转换期间出错无日期

时间:2014-08-20 13:14:15

标签: sql datetime-format

最后一个字段 STEPCD varchar(10)

如果我删除where语句smsdss.c_cfvhs_EMSTAT_ORDERS.STEPCD = '10006',则查询运行正常。但是当我添加它时,我得到了错误:

 Conversion failed when converting date and/or time from character string. 

如果STEP代码是varchar,那么这个日期/时间错误来自何处?

Select CP_Population.*, CHRTNO,PRSNTINGCOMPL,DISPOCD,

CONVERT(DATETIME, CAST(smsdss.c_cfvhs_EMSTAT_CHART.ARRVDATE AS datetime)    
+ CAST(smsdss.c_cfvhs_EMSTAT_CHART.ARRVTIME AS time), 120) AS [ED_Arrival_dtime],

CONVERT(DATETIME, CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHDATE AS datetime)    
+ CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHTIME AS time), 120) AS [ED_Discharge_dtime],

DATEDIFF(MINUTE,CONVERT(DATETIME, CAST(smsdss.c_cfvhs_EMSTAT_CHART.ARRVDATE AS datetime)    
+ CAST(smsdss.c_cfvhs_EMSTAT_CHART.ARRVTIME AS time), 120),
CONVERT(DATETIME, CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHDATE AS datetime)    
+ CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHTIME AS time), 120)) AS [ED_Arrival to ED_Discharge],

CASE 
WHEN CP_Population.vst_type_cd = 'O' 
THEN DATEDIFF(MINUTE,CONVERT(DATETIME, CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHDATE AS datetime)   
+ CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHTIME AS time), 120), CP_Population.op_released)*1.0/60  
WHEN CP_Population.vst_type_cd = 'I' 
THEN DATEDIFF(MINUTE,CONVERT(DATETIME, CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHDATE AS datetime)   
+ CAST(smsdss.c_cfvhs_EMSTAT_CHART.DSCHTIME AS time), 120), CP_Population.vst_end_dtime)*1.0/60           ELSE NULL END 
As [Unit_LOS],
STEPCD

FROM CP_Population INNER JOIN smsdss.c_cfvhs_EMSTAT_CHART
ON right('00' + smsdss.c_cfvhs_EMSTAT_CHART.VISITNO,12) COLLATE SQL_Latin1_General_Pref_CP1_CI_AS     = CP_Population.pt_id

LEFT OUTER JOIN smsdss.c_cfvhs_EMSTAT_ORDERS ON smsdss.c_cfvhs_EMSTAT_CHART.CHRTNO =     smsdss.c_cfvhs_EMSTAT_ORDERS.CHARTNO
LEFT OUTER JOIN smsdss.c_cfvhs_EMSTAT_DISPO ON smsdss.c_cfvhs_EMSTAT_CHART.DISPOCD =   smsdss.c_cfvhs_EMSTAT_DISPO.CODE

WHERE TYPE = 'A'
AND smsdss.c_cfvhs_EMSTAT_ORDERS.STEPCD = '10006' <--ERROR

1 个答案:

答案 0 :(得分:0)

我认为您的STEPCD列中有一条错误记录,如果您使用的是SQL 2012,请尝试使用TRY_CONVERT并检查结果以查看哪一行返回null,这将是您的脏记录。

您可以尝试的其他选项是在查询中添加以下where子句

WHERE TYPE = 'A'
AND smsdss.c_cfvhs_EMSTAT_ORDERS.STEPCD = '10006' 
AND ISDATE(smsdss.c_cfvhs_EMSTAT_ORDERS.STEPCD) = 0