I have 2 date attributes stored as text strings that need to be compared with earliest date selected.
In the output shown here, you can see for ID 1 the earliest date has not been correctly identified using my code.
I have tried using casting and creating new columns but cannot find a way to compare the 2 date attributes in their current format, please can someone advise on how to do this.
Current query:
SELECT DISTINCT
A.[ID],
A.[Date field 1 ],
B.[Date field 2],
CASE
WHEN A.[Date field 1 ] < B.[Date field 2]
THEN (A.[Date field 1 ])
ELSE [Date field 2]
END AS 'Earliest date'
FROM
[TABLE A] A
JOIN
[TABLE B] B ON A.[ID] = B.[ID]
GROUP BY
A.[ID], A.[Date field 1 ], B.[Date field 2]
Output:
ID Date field 1 Date field 2 Earliest date
------------------------------------------------------------
1 02/08/2025 04:00 25/07/2025 15:58 02/08/2025 04:00
2 14/07/2025 04:08 25/07/2025 16:02 14/07/2025 04:08
3 14/07/2025 04:03 25/07/2025 16:11 14/07/2025 04:03
4 14/07/2025 04:03 25/07/2025 16:03 14/07/2025 04:03
'08/08/2025'is BEFORE'12/09/1984'.09/01/2025would be considered greater than01/12/2025