Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 368283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:48:32+00:00 2026-05-12T13:48:32+00:00

Ok, firstly I’ve seen this thread . But none of the solutions are very

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:48:33+00:00Added an answer on May 12, 2026 at 1:48 pm

    I would pass the Dates in XML (you could use varchar/etc, and convert to the xml datatype too):

    DECLARE @output DateTime
    DECLARE @test XML
        SET @test = '<VALUES><VALUE>1</VALUE><VALUE>2</VALUE></VALUES>'
    
    DECLARE @docHandle int 
    EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc 
    
    SET @output = SELECT MAX(TheDate)
                    FROM (SELECT t.value('./VALUE[1]','DateTime') AS 'TheDate'
                            FROM OPENXML(@docHandle, '//VALUES', 1) t)
    
    EXEC sp_xml_removedocument @docHandle
    
    RETURN @output
    

    That would address the issue of handling as many possibilities, and I wouldn’t bother putting nulls in the xml.

    I’d use a separate parameter to specify the datetype rather than customize the xml & supporting code every time, but you might need to use dynamic SQL for it to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, this is going to sound like homework, but it ain't. Just a problem
Firstly, very sorry if this is not a true stackoverflow question. But it's something
Firstly I have a class signature that looks like this: internal class JobStore :
Firstly, I wrote my customized setter for an NSString* like this: - (void)setDateString:(NSString *)newDateString
Firstly sorry if this is a common question but I couldn't find anything on
Firstly, I have search Stack Overflow for the answer, but I have not found
Firstly, this is a homework assignment, and I am very new to programming in
Firstly my maths is limited, so this question may have a simple answer. So,
Firstly I have tried to re create this within a JSfiddle but unable to,
Firstly, I know this [type of] question is frequently asked, so let me preface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.