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 9090951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:29:42+00:00 2026-06-16T22:29:42+00:00

Suppose I have a source that I have to query like this: Select Fields

  • 0

Suppose I have a source that I have to query like this:

Select Fields 
from TableA A
join TableB B on A.SomeField = B.SomeField
join TableC C on B.SomeField = C.SomeField
Join TableD D on C.SomeField = D.SomeField
Where 
    CustomMaxDateFunction (A.SomeDateField, B.SomeDateField, C.SomeDateField, D.SomeDateField) > '20130101 23:59:00'

I know I could write the where clause as something like the following but I’d rather not:

Where 
  A.SomeDateField > '20130101 23:59:00' 
  OR B.SomeDateField > '20130101 23:59:00' 
  OR C.SomeDateField > '20130101 23:59:00' 
  OR D.SomeDateField > '20130101 23:59:00'

Keep in mind that the number of columns to compare would be variable based upon the number of source tables in the join.

Any suggestions as to the least cumbersome way to do this since we’ll have to use the custom function (or stored procedure) repeatedly.

  • 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-06-16T22:29:43+00:00Added an answer on June 16, 2026 at 10:29 pm

    Instead of creating a new function you can just use the build-in MAX aggregate.

    SELECT MAX(v) FROM (VALUES(A.SomeDateField),(B.SomeDateFiled),(C.SomeDateField),(D.SomeDateField))dates(v)
    

    or in context of your query:

    SELECT *
    FROM TableA AS A
    JOIN TableB AS B
    ON ...
    JOIN TableC AS C
    ON ...
    JOIN TableD AS D
    ON ...
    WHERE (SELECT MAX(v) FROM (VALUES(A.SomeDateField),(B.SomeDateFiled),(C.SomeDateField),(D.SomeDateField))dates(v)) > '20130101 23:59:00'
    

    If that is not clean enough, you can wrap it into a function

    CREATE FUNCTION dbo.CustomMaxDateFunction(@v1 DATETIME, @v2 DATETIME, @v3 DATETIME, @v4 DATETIME)
    RETURNS TABLE
    AS
      RETURN SELECT MAX(v) MaxDate FROM (VALUES(@v1),(@v2),(@v3),(@v4))dates(v);
    

    And then call it like this:

    SELECT *
    FROM TableA AS A
    JOIN TableB AS B
    ON ...
    JOIN TableC AS C
    ON ...
    JOIN TableD AS D
    ON ...
    CROSS APPLY dbo.CustomMaxDateFunction(A.SomeDateField, B.SomeDateFiled, C.SomeDateField, D.SomeDateField) AS mdf
    WHERE mdf.MaxDate > '20130101 23:59:00'
    

    Stay away from other forms of functions in this query context as they will hurt performance:
    http://sqlity.net/en/498/t-sql-tuesday-24-prox-n-funx/

    All functions have in common however, that you are fixed to a given number of parameters, so the first option is probably your best bet.

    EDIT:
    If you really like the idea of a function you could do this:

    CREATE FUNCTION dbo.CustomMaxDateFunction(@v1 DATETIME, @v2 DATETIME, @v3 DATETIME, @v4 DATETIME)
        RETURNS TABLE
        AS
          RETURN SELECT MAX(v) MaxDate FROM (VALUES(@v1),(@v2),(@v3),(@v4))dates(v) WHERE v IS NOT NULL;
    

    Now you can call it like this:

    SELECT *
    FROM TableA AS A
    JOIN TableB AS B
    ON ...
    CROSS APPLY dbo.CustomMaxDateFunction(A.SomeDateField, B.SomeDateFiled, NULL, NULL) AS mdf
    WHERE mdf.MaxDate > '20130101 23:59:00'
    

    You essentially have to pad the unused parameters with NULL. The additional WHERE clause in the function prevents the warning about eliminated NULL values you would get otherwise. As RBarryYoung mentioned in the comments, there is no real optional parameter functionality for functions in SQL Server.

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

Sidebar

Related Questions

Suppose that i have this HTML from a source (scrapping it) : <tr class=calendar_row
Suppose you have some source code that comes from the unix world. This source
Suppose I have a source example document like this: <html> <b><i><u>TestBIU</u></i></b> <i><b><u>TestIBU</u></b></i> <i><u><b>TestIUB</b></u></i> <b><u><i>TestBUI</i></u></b>
Suppose I have a source file that is 18218 bytes. I open the file
Suppose I have two lists that holds the list of source file names and
suppose that,we have following code auto_ptr<T> source() { return auto_ptr<T>( new T(1) ); }
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
Suppose I have this database table (some sample code below) that stores the relationship
Suppose now I have three source files: ClassA.hpp, ClassB.hpp, and ClassC.hpp. ClassB inherits from
Suppose I have a destination class and a source class that are mostly the

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.