我有一個日期時間列,我正在檢查null。我想要做的就是讓它說「沒有下一個約會」,如果該列爲空。然而我得到一個錯誤:SQL - 從字符串中轉換日期和/或時間時出錯
conversion failed when converting date and/or time from character string
我不知道爲什麼。我嘗試了幾種不同的方式來編寫查詢,但它一直給我提供相同的錯誤。
這裏是我的query
SELECT
t1.PatientID,
t1.FullName,
t1.CurrentRAF_DW,
t1.NumberOfCompletedClinicVisits,
t1.PreferredServiceLocation,
case when t1.IsVip = 1 then 'Yes' else 'No' end as [IsVip],
case when t1.PatientTier = 'Critical' then 'Every Three Weeks'
when t1.PatientTier = 'Serious' then 'Every 1 Month'
when t1.PatientTier = 'Fair' then 'Every 2 Months'
when t1.PatientTier = 'Good' then 'Every 3 Months'
else ' '
end as [Cadence],
t1.PatientTier,
t2.hcc_18,
t2.hcc_19,
t2.hcc_21,
t2.hcc_22,
t2.hcc_12,
t2.hcc_136,
t2.hcc_137,
t2.hcc_108,
t2.hcc_88,
t2.hcc_111,
t2.hcc_85,
t2.hcc_55,
t1.LastCompletedClinicVisit,
case when t1.NextScheduledClinicVisit is not null then t1.NextScheduledClinicVisit else 'No Next Appointment' end as [NextScheduledVisit]
FROM vw_patient_attributes t1
INNER JOIN STG_OSHODS_DW.osh_rpt.dim_member_care_measures t2
ON t1.PatientID = t2.emr_id
在這裏討論的列是 「NextScheduledClinicVisit」。
列'[NextScheduledVisit]'不能是兩種數據類型。將t1.NextScheduledClinicVisit轉換爲varchar,以便它和您的文本'No Next Appointment'都是varchar數據。類似於'CONVERT(VARCHAR(50),t1.NextScheduledClincVisit,101)'而不是 – xQbert
'當t1.NextScheduledClinicVisit不爲空然後強制轉換(t1.NextScheduledClinicVisit as varchar(40))else'No Next Appointment'end as [NextScheduledVisit ]' – Hackerman