我正在尝试使用比较两个日期的CASE statemetn写入脚本。例如,如果患者在预约日期后取消预约,则应在“之后”阅读;如果患者在预约日期之前取消,则应在“之前”阅读。我怎么能做到这一点?
示例(以下不起作用):
Select
PatientName, PatientFirstName, PatientLastName, PatientSSN, Age, Gender,
AppointmentDateTime, CancelDateTime, CancelNoShowCode, Appointment_Status,
CancellationReason, CancellationRemarks, LocationName, InstitutionName,
DivisionName, AppointmentSID,
CASE WHEN CancelDateTime > 'AppointmentDateTime' then 'after'
WHEN CancelDateTime < 'AppointmentDateTime' then 'BEFORE'
END as WHENCANCELLED
from #mhvisits2
where CancelDateTime is NOT null
order by PatientName
答案 0 :(得分:0)
选择
PatientName,PatientFirstName,PatientLastName,PatientSSN,年龄,性别,
AppointmentDateTime,CancelDateTime,CancelNoShowCode,Appointment_Status,
CancellationReason,CancellationRemarks,LocationName,InstitutionName,
DivisionName,AppointmentSID,
CASE
WHEN CancelDateTime&gt;约会日期然后'之后'
当CancelDateTime&lt;约会日期然后“之前”
ELSE''
结束为WHENCANCELLED
其中CancelDateTime不为null
按PatientName排序
您在CASE语句中缺少ELSE条件,并从列名中删除任何(')。