Link to home
Start Free TrialLog in
Avatar of vtechdev
vtechdevFlag for United States of America

asked on

Return latest of 2 dates

What is the best way to compare 2 dates and return the larger date when it cannot be known in advace if both or even either date will exist.
If no dates exist I expect a null, if only 1 date exists I expect that date, but if both exist I expect the later date.
Avatar of Aneesh
Aneesh
Flag of Canada image

SELECT MAX(urDateColumn) from urTable
Avatar of vtechdev

ASKER

Let me clarify, I really want to write a User Defined Function(UDF) to return the later of 2 dates, but the UDF must allow 1 or both of the dates to be null and still function. I don't want the MAX from a table.
Or, if you want to manipulate the data later:

SELECT yourIdField, MAX(yourDateColumn) as yourMaxDate FROM yourTable GROUP BY yourIdField

This will return a list of unique IDs, each one being the most recent date.  Conversely, you could use MIN() to select the oldest.
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I'm tired and the simplest and best solution was escaping me, thanks for providing it quickly.