2013-10-02 173 views
-2

我想写一个脚本使用CASE statemetn来比较两个日期。例如,如果患者在预约日期之后取消预约,则应该在“之后”读取;如果患者在预约日期之前取消,则应在“之前”阅读。我怎样才能做到这一点?SQL CASE语句与日期标准

例(以下没有工作):

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 
+2

你有一个名为'AppointmentDateTime'的列吗?如果是,那么你的查询工作所需要的就是从AppointmentDateTime删除单引号(''') – Lamak

回答

0

选择
PatientName,PatientFirstName,PatientLastName,PatientSSN,年龄,性别, AppointmentDateTime,CancelDateTime,CancelNoShowCode,Appointment_Status, CancellationReason,CancellationRemarks, LOCATIONNAME,InstitutionName, DivisionName,AppointmentSID,
CASE
WHEN CancelDateTime> AppointmentDateTime然后 '后'
WH EN CancelDateTime < AppointmentDateTime那么 '前'
ELSE ''
END从#mhvisits2
其中CancelDateTime不为空 为了通过PatientName

你缺少else条件在您的CASE语句和WHENCANCELLED

从列名称中删除任何(')。