By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE #indebtedness (call_case CHAR(10), date1 DATETIME, date2 DATETIME, date3 DATETIME);
INSERT #indebtedness VALUES ('Key1', '2019-10-30', '2019-11-30', '2019-10-25');
INSERT #indebtedness VALUES ('Key2', NULL, '2019-11-30', '2019-10-25');
INSERT #indebtedness VALUES ('Key3', '2019-10-30', NULL, '2019-10-25');
INSERT #indebtedness VALUES ('Key4', '2019-10-30', '2019-11-30', NULL);
INSERT #indebtedness VALUES ('Key5', NULL, NULL, '2019-10-25');
INSERT #indebtedness VALUES ('Key6', NULL, '2019-11-30', NULL);
INSERT #indebtedness VALUES ('Key7', '2019-10-30', NULL, NULL);
INSERT #indebtedness VALUES ('Key8', NULL, NULL, NULL);
SELECT
call_case,
CASE WHEN (date1 > date2 OR date2 IS NULL) AND (date1 > date3 OR date3 IS NULL)
THEN date1
WHEN date2 > date3 OR date3 IS NULL
THEN date2
ELSE date3 END AS [Latest Date]
FROM #indebtedness;
call_case | Latest Date |
---|---|
Key1 | 2019-11-30 00:00:00.000 |
Key2 | 2019-11-30 00:00:00.000 |
Key3 | 2019-10-30 00:00:00.000 |
Key4 | 2019-11-30 00:00:00.000 |
Key5 | 2019-10-25 00:00:00.000 |
Key6 | 2019-11-30 00:00:00.000 |
Key7 | 2019-10-30 00:00:00.000 |
Key8 | null |