I have a datetime column which I'm checking for null. All I want to do is have it say "No Next Appointment" if the column is null. However I'm getting an error:
conversion failed when converting date and/or time from character string
I'm not sure why. I tried a couple different ways to write the query but it keeps giving me the same error.
Here is my 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
The column in question here is "NextScheduledClinicVisit".
[NextScheduledVisit]can't be two data types. Cast t1.NextScheduledClinicVisit to a varchar so that it and your text 'No Next Appointment' are both varchar data. Something likeCONVERT(VARCHAR(50), t1.NextScheduledClincVisit, 101)insteadcase when t1.NextScheduledClinicVisit is not null then cast(t1.NextScheduledClinicVisit as varchar(40)) else 'No Next Appointment' end as [NextScheduledVisit]