0

我对以下代码有问题:

USE [PCIPNY];

insert into [dbo].[test_Drug_Medication_1]
(
       [MedicationID] ,[ClinicID] ,[PatientID] ,[MyDrugID] ,[NDC] ,[DoctorID] ,[DrugID] ,[DrugFullDesc] ,[Generic_Name]
      ,[Print_Name] ,[Manufacture] ,[Inactive_Date] ,[Strength] ,[Units] ,[Pkg_Size] ,[Pkg_Type] ,[Route] ,[Take]
      ,[Frequency] ,[Duration] ,[Qualifier] ,[Quantity] ,[Refill] ,[Note] ,[DAW] ,[CreateDate] ,[CreateBy]
      ,[ModifyBy],[IsControl] ,[UserDefine] ,[scheduleLevel] ,[BeginDate] ,[EndDate] ,[Active] ,[sente]
      ,[OnCologyCheckStatus] ,[OnCologyCheckStatus1] ,[OnCologyCheckStatus2] ,[SIG] ,[Printed] ,[ICDCode] ,[SendeFaxed] ,[DosageForm] ,[GPI]
      ,[IsBrand] ,[IsGeneric] ,[GenericAndBrand] ,[SigUnit] ,[IsDrug] ,[Status]
)

SELECT
      [MedicationID] ,[ClinicID] ,[PatientID] ,[MyDrugID] ,[NDC] ,[DoctorID] ,[DrugID] ,[DrugFullDesc] ,[Generic_Name]
      ,[Print_Name] ,[Manufacture] ,[Inactive_Date] ,[Strength] ,[Units] ,[Pkg_Size] ,[Pkg_Type] ,[Route] ,[Take]
      ,[Frequency] ,[Duration] ,[Qualifier] ,[Quantity] ,[Refill] ,[Note] ,[DAW] ,[CreateDate] ,[CreateBy]
      ,[ModifyBy] ,[IsControl] ,[UserDefine] ,[scheduleLevel] ,[BeginDate] ,[EndDate] ,[Active] ,[sente]
      ,[OnCologyCheckStatus] ,[OnCologyCheckStatus1] ,[OnCologyCheckStatus2] ,[SIG] ,[Printed] ,[ICDCode] ,[SendeFaxed] ,[DosageForm] ,[GPI]
      ,[IsBrand] ,[IsGeneric] ,[GenericAndBrand] ,[SigUnit] ,[IsDrug] ,[Status]

  FROM [ec14].[dbo].[Drug_Medication]
    where 1=1
      and clinicid in (select clinicid from [dbo].[clinic] where org_db = 'ec14' and ClinicID=1234);

我收到了这个错误:

消息 241,级别 16,状态 1,第 3 行
从字符串转换日期和/或时间时转换失败。

我发现错误在列中begindate

begindateec04(原始位置)中的列是charvar(16)数据类型。PCIPNY(destination location) 中
的列是数据类型。begindatedatetime

有什么解决方案可以完成这项工作吗?

4

3 回答 3

1

In the SELECT clause from source, change

BeginDate

To

CASE WHEN ISDATE(BeginDate) = 1 then CAST(BeginDate AS DATETIME) END

It is a case of not setting the DMY/MDY format correct? You can try changing SET DATEFORMAT. Or are all the dates (in varchar) in a specific format that can actually be converted? If so you can use CONVERT with a format specifier.

CONVERT(datetime, BeginDate, 103)
于 2012-10-12T17:14:22.933 回答
0

尝试投射您从 ec14 中选择的字段 ..

, cast(begindate as datetime) ,

于 2012-10-12T17:13:45.400 回答
0

确保日期格式正确,或者列没有移动。当插入的列的顺序错误之前,我遇到过这个错误。尝试进入日期字段的数据可能不是日期和另一个字段。

于 2012-10-12T17:13:49.330 回答