将varchar数据类型转换为datetime数据类型会导致超出范围的值

时间:2012-06-04 09:01:11

标签: sql sql-server

将varchar数据类型转换为datetime数据类型会导致超出范围的值

select a.DLNO,
       a.NAME,
       b.TOPSTRING,
       Convert(datetime,a.DOB,103) as DOB, 
       Convert(datetime,a.DOI,103) as DOI,
       Convert(datetime,b.datepushed,103) as datepushed 
from    
       PLInfo_Demo a,DLInfo_Demo b 
where 
       a.dlno=b.DLNO

2 个答案:

答案 0 :(得分:7)

类型103要求您拥有欧洲日期/月份订单的日期时间:'dd/mm/yyyy'

如果您先存储月份,则可能会导致此错误(例如'01/13/2012'

如果是这种情况,请使用101'mm/dd/yyyy'

类型

将日期时间存储为DATETIME总是更好。

答案 1 :(得分:0)

sp_helptext sp_displayClinicRecordsbyclinicid


alter procedure sp_displayClinicRecordsbyclinicid

(

@id bigint,

@category char(1)

)

as


select id,ClinicName,

CONVERT(varchar(10), cast(Visitdate as datetime),101) Visitdate ,

CONVERT(varchar(19), cast(visitTime as datetime) ,101)  visitTime ,

patient_first,patient_last,patient_address,patient_city,patient_state, CONVERT(varchar(10), cast(patient_dob as datetime),101) patient_dob,



precriber,prescriptionnum,CONVERT(varchar(10),cast(pharmacyfilldate as datetime) ,101)  pharmacyfilldate,CONVERT(varchar(10),cast(prescriptionenddate as datetime) ,101) prescriptionenddate,drugname,drugformat,npi,dea,



licence,clinicid,Category,PatientFullname,Prescriber_first,Prescriber_last,Qty,Days



 from tblClinicRecords where clinicid=@id and Category=@category