2013-10-02 3 views
-2

두 날짜를 비교하는 CASE statemetn을 사용하여 스크립트에 쓰려고합니다. 예를 들어, 약속 날짜 이후에 환자가 약속을 취소하면 '이후'를 읽어야합니다. 환자가 약속 날짜 이전에 취소하면 'before'라고 읽어야합니다. 이것을 어떻게 할 수 있습니까?날짜 조건이 포함 된 사례 코드

예 (아래 작동하지 않았다) :

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,
사례
WHEN CancelDateTime> AppointmentDateTime then 'after'
WH EN CancelDateTime < AppointmentDateTime 다음 ELSE
''
END '전'CancelDateTime 당신은 당신의 CASE 문에서 ELSE 조건을 누락하는 널 (null) PatientName 에 의해 순서

없는 경우에는 # mhvisits2
에서 WHENCANCELLED

로 열 이름에서 any (')를 제거하십시오.

관련 문제