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

  • Home
  • SEARCH
  • 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 813209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:19:22+00:00 2026-05-15T01:19:22+00:00

I am comparing two dates and trying to determine the max of the two

  • 0

I am comparing two dates and trying to determine the max of the two dates. A null date would be considered less than a valid date. I am using the following case statement, which works – but feels very inefficient and clunky. Is there a better way?

update @TEMP_EARNED
set nextearn = case when lastoccurrence is null and lastearned is null then null
                   when lastoccurrence is null then lastearned
                   when lastearned is null then lastoccurrence
                   when lastoccurrence > lastearned then lastoccurrence
                   else lastearned end; 

(This is in MS SQL 2000, FYI.)

  • 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-15T01:19:22+00:00Added an answer on May 15, 2026 at 1:19 am
    select c1, c2, 
           case when c1 > c2 then c1 else coalesce(c2,c1) end as max 
    from twocol;
    +------+------+------+
    | c1   | c2   | max  |
    +------+------+------+
    | NULL | NULL | NULL |
    | NULL |    2 |    2 |
    |    1 | NULL |    1 |
    |    1 |    2 |    2 |
    | NULL | NULL | NULL |
    |    2 | NULL |    2 |
    | NULL |    1 |    1 |
    |    2 |    1 |    2 |
    +------+------+------+
    

    Why does this work? if neither operand is null, then we get a “normal” comparison: the “then” branch when c1 > c2, the else branch when c1 <= c2.

    On the “else” branch, we call coalesce, but as its first argument is the non-null c2, we return c2.

    .

    But if either operand is null, the test c1 > c2 evaluates to false, and we return coalesce( c2, c1 ).

    If the null operand was c1, we get c2, which is what we want because we are (for this question) calling null is “less than” any non-null value.

    If the null operand was c2, we get c1. That’s fine, because c1 is either not null, and thus (for this question) “greater than” the null c2, or…

    If both operands were null, we get c1, but it doesn’t matter which we get, as both are null.

    An additional advantage is that this idiom works for any types for which operator > works, without further thought or details.

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

Sidebar

Related Questions

Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
What is the most efficient/recommended way of comparing two NSDates? I would like to
I'm comparing two dates and my code goes like this. jQuery('.newAppointment a.ui-state-default').click(function() { var
Possible Duplicate: How to find number of days between two dates using php I'm
I am having a problem when i am trying to compare two dates in
Possible Duplicates: Comparing dates How to compare two dates in Objective-C i want to
I've been having some 'strange' results while comparing dates. table1 has two rows with
Is there an easy way of comparing weeks in two dates like this... var
I want to compare two dates in PHP. One date is the date due,
I am comparing the date when a file was last modified for two files,

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.