2

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
4
  • 1
    Did you consider using Cast or Convert on your date strings before comparing them? Commented Aug 8 at 13:22
  • 2
    "There are 2 date attributes stored as text strings that need to be compared with earliest date selected." Fixing your design should be your priority. Storing dates as a string is a recipe for disaster. According to your data, the "date" '08/08/2025' is BEFORE '12/09/1984'. Commented Aug 8 at 13:25
  • Show source data (not only output) and [Table A], [Table B] definitions. Commented Aug 8 at 13:26
  • 1
    Is your output showing the actual format in which those values are stored? Of course you can't sort them properly as strings then - because string comparison happens character-by-character, from left to right. So f.e. 09/01/2025 would be considered greater than 01/12/2025 Commented Aug 8 at 13:27

3 Answers 3

2

If you keep your current design this is one correction you could use: The date strings need to be converted to the DATETIME datatype for proper comparison. I chose style 103 (British/French) because I saw 14/07/2025. I use TRY_CONVERT because of the possible formatting errors identified by Thom A.

--Corrected Original Query
SELECT DISTINCT 
  A.[ID],
  A.[Date field 1 ],
  B.[Date field 2],
  CASE 
  WHEN TRY_CONVERT(DATETIME,A.[Date field 1 ],103) IS NULL THEN 'Invalid date field 1'
  WHEN TRY_CONVERT(DATETIME,B.[Date field 2],103) IS NULL THEN 'Invalid date field 2'
  WHEN CONVERT(DATETIME,A.[Date field 1 ],103) < CONVERT(DATETIME,B.[Date field 2],103) 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]

fiddle

ID Date field 1 Date field 2 Earliest date
1 02/08/2025 04:00 25/07/2025 15:58 25/07/2025 15:58
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
5 31/02/2025 04:03 31/02/2025 16:03 Invalid date field 1
6 14/07/2025 04:03 14/07/2025 04:02 14/07/2025 04:02
7 14/07/2025 04:02 14/07/2025 04:03 14/07/2025 04:02
Sign up to request clarification or add additional context in comments.

4 Comments

Here's hoping there no dates like '31/02/2025', '07/13/2025', '321468324' or 'I like potato' :)
@ThomA well it wont give a wrong result, it will give none
It'll error, more specifically, @vaivel .
@thom-a My point exactly :)
2

The problem here is your design, you need to fix it. Storing dates as a string is an extremely bad flaw, as there's nothing stopping someone entering complete nonsense for a "date", and especially when that date isn't in the format yyyyMMdd, as the dates don't order correctly. For example the "date" '08/08/2025' is BEFORE '12/09/1984', because '0' is less than '1'.

Let's start with a sample table and data:

CREATE TABLE dbo.YourTable (ID int,
                            Date1 varchar(16),
                            Date2 varchar(16));
GO
INSERT INTO dbo.YourTable (ID,
                           Date1,
                           Date2)
VALUES(1,'02/08/2025 04:00','25/07/2025 15:58'),
      (2,'14/07/2025 04:08','25/07/2025 16:02'),
      (3,'14/07/2025 04:03','25/07/2025 16:11'),
      (4,'14/07/2025 04:03','25/07/2025 16:03');

Step one would be to add 2 new columns of the correct data type; datetime2(0) seems appropriate here:

ALTER TABLE dbo.YourTable ADD ActualDate1 datetime2(0), ActualDate2 datetime2(0);

Then we can UPDATE those columns to have an actual date value. I use TRY_CONVERT here as if you have any invalid dates, which is completely possible with your design, we will NULL those.

UPDATE dbo.YourTable
SET ActualDate1 = TRY_CONVERT(date,Date1,103),
    ActualDate2 = TRY_CONVERT(date,Date2,103);

Then we can "play" some rename games; I rename the old columns to a string value so that if there are any invalid dates, they are at least still available (you can DROP the columns later after you fix the data), and then rename the new columns to the old columns names:

EXEC sp_rename N'dbo.YourTable.Date1', N'Date1String', N'COLUMN';
EXEC sp_rename N'dbo.YourTable.Date2', N'Date2String', N'COLUMN';
EXEC sp_rename N'dbo.YourTable.ActualDate1', N'Date1', N'COLUMN';
EXEC sp_rename N'dbo.YourTable.ActualDate2', N'Date2', N'COLUMN';

Finally, then, you can just use LEAST to get the lowest value:

SELECT ID,
       Date1,
       Date2,
       LEAST(Date1, Date2) AS EarliestDate
FROM dbo.YourTable
--clean up
DROP TABLE dbo.YourTable;

Comments

0

You are comparing two date columns stored as text strings, like '02/08/2025 04:00' and '25/07/2025 15:58', but the comparison is done as string comparison, not date comparison. This causes incorrect results because string comparison compares character-by-character rather than chronologically.

Why it happens:
When you do:

CASE WHEN A.[Date field 1 ] < B.[Date field 2] THEN (A.[Date field 1 ])
ELSE [Date field 2]
END AS 'Earliest date'

and the fields are strings, SQL compares '02/08/2025 04:00' and '25/07/2025 15:58' lexicographically (alphabetical order), not by date. So '0' < '2' makes the first string seem "smaller" even if the date is actually later.


How to fix it: Convert the strings to proper date/datetime types before comparing

If you're using SQL Server, and your dates are in dd/mm/yyyy hh:mm format, you can convert them using CONVERT with style 103:

CONVERT(datetime, [DateString], 103) 

Corrected query example:

SELECT DISTINCT 
    A.[ID],
    A.[Date field 1],
    B.[Date field 2],
    CASE 
        WHEN CONVERT(datetime, A.[Date field 1], 103) < CONVERT(datetime, B.[Date field 2], 103) 
            THEN A.[Date field 1] 
        ELSE 
            B.[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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.