Ok, firstly I've seen this thread. But none of the solutions are very satisfactory. The nominated answer looks like NULLs would break it, and the highest-rated answer looks nasty to maintain. So I was wondering about something like the following :
CREATE FUNCTION GetMaxDates
(
@dte1 datetime,
@dte2 datetime,
@dte3 datetime,
@dte4 datetime,
@dte5 datetime
)
RETURNS datetime
AS
BEGIN
RETURN (SELECT Max(TheDate)
FROM
(
SELECT @dte1 AS TheDate
UNION ALL
SELECT @dte2 AS TheDate
UNION ALL
SELECT @dte3 AS TheDate
UNION ALL
SELECT @dte4 AS TheDate
UNION ALL
SELECT @dte5 AS TheDate) AS Dates
)
END
GO
Main problems I see are that if there are only 3 fields to compare, you'd still have to specify NULL for the other 2, and if you wanted to extend it to six comparisons it would break existing use. If it was a parameterized stored procedure you could specify a default for each parameter, and adding new parameters wouldn't break existing references. The same method could also obviously be extended to other datatypes or stuff like Min or Avg. Is there some major drawback to this that I'm not spotting? Note that this function works whether some, all or none of the values passed to it are nulls or duplicates.