日期:2014-05-18  浏览次数:21040 次

从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。

Select distinct cast(a.patientID as varchar(20) ) as patientID,
cast(a.patienRecordNo as varchar(50) ) as patientRecordNo,
cast(a.patientname as varchar(20)) as patientname,
a.sex,
cast(a.birthday as datetime) as birthday,
cast((a.age/365) as tinyint)as age,
cast(a.nation as varchar(18)) as nation, 
cast(a.idcard as varchar(18)) as idcard,
case when left(cast(a.RelationTel as varchar(50)),1)='1' then RelationTel end as mobile,
cast(a.job as varchar(50)) as job,
cast(a.company as varchar(50)) as company,
cast(a.CompanyPhone as varchar(30)) as companyphone,
cast(a.RPRAddr as varchar(500)) as RPRAddr,
case when left(cast(a.RelationTel as varchar(50)),1)!='1' then RelationTel end as RPRTel,
cast(a.RelationName as varchar(20)) as relationName,
cast(a.relation as varchar(20)) as relation,
cast(a.PayType as varchar(40)) as paytype,
cast(substring(cast(a.InDays as varchar),1,2) as smallint(2)) as indays,
CONVERT(VARCHAR(10),a.EnterDate,120) as enterdate,
cast(a.enterdept as varchar(40)) as enterdept,
CONVERT(VARCHAR(10),a.outdate,120) as outdate,
cast(a.outdept as varchar(60)) as outdept,
cast(a.OutDiag as varchar(500)) as outdiag,
cast(a.clinicdct as varchar(20)) as clinicdct,
cast(a.attendingmedic as varchar(20)) as attendingmedic,
cast(a.hospitalmedic as varchar(20)) as hospitalmedic,
cast(a.TotalFee as money) as totalfee,
cast(a.treat_result as varchar(500)) as treat_result
From Openquery(dataorcl,'Select * From V_PAT_VISIT_INFO ') a 
   
  where a.patienRecordNo is not null and outdate is not null and left(ltrim(rtrim(RelationTel)),1)>='0' and a.outdate>getdate()-9 and a.outdate<getdate()-2  
  and not exists(select * from fwd_patient where fwd_patient.patienRecordNo=cast(a.patienRecordNo as varchar(50)) and fwd_patient.name=a.patientname and a.outdate>getdate()-9 and a.outdate<getdate()-2 )



在存储过程中执行这段话就报错


------解决方案--------------------
a.birthday这个字段有值不能转换为datetime类型。