簡體   English   中英

SQL Server 2008:將varchar轉換為datetime失敗

[英]SQL server 2008: Conversion of varchar to datetime failed

我的以下代碼有問題:

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列中。

begindate在ec04(原點位置)是charvar(16)的數據類型。
begindate在PCIPNY(目標位置)是datetime數據類型。

有什么解決方案可以使這項工作嗎?

在源中的SELECT子句中,更改

BeginDate

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

是否存在未正確設置DMY / MDY格式的情況? 您可以嘗試更改SET DATEFORMAT 還是所有日期(以varchar格式)都可以實際轉換為特定格式? 如果是這樣,您可以將CONVERT與格式說明符一起使用。

CONVERT(datetime, BeginDate, 103)

嘗試投射從ec14中選擇的字段。

, cast(begindate as datetime) ,

確保日期格式正確,或者列沒有移位。 當插入的列順序錯誤時,我遇到了此錯誤。 嘗試輸入日期字段的數據可能不是日期和另一個字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM