(
Select Top 1 case isdate(refdate)
when 1 then convert(char(10), convert(datetime, refdate,112), 101)
Else '01/01/1899'
End refDate from
(
SELECT declinedDate refDate
FROM dbo.order_ AS o2 with (nolock)
WHERE o2.enterprise_id = '00001' and o2.practice_id = '0001' and o2.person_id = p.person_id
AND (actCode IN ('90718')) AND
(declinedDate IS NOT NULL) AND (isdate(declinedDate) = 1) and (declinedDate BETWEEN Convert(char(8), DateAdd(yy,-10, @e),112) AND @e)
union
SELECT cancelledDate refDate
FROM dbo.order_ AS o2 with (nolock)
WHERE o2.enterprise_id = '00001' and o2.practice_id = '0001' and o2.person_id = p.person_id
AND (actCode IN ('90718')) AND
(cancelledDate IS NOT NULL) AND (isdate(cancelledDate) = 1) and (cancelledDate BETWEEN Convert(char(8), DateAdd(yy,-10, @e),112) AND @e)
) TdPast10yearsRefuse
order by refDate desc
)
AS TdPast10yearsRefusedDate,
|