• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

SQL date eval, choose the lowest out of 3 date columns

Hi Experts,

I am trying to figure out a way to evaluate 3 different date columns and select the lowest time value.  I also have NULL's in these fields

For example

       A                                      b                                  c
1/1/2010 1:51               1/1/2010 13:51          1/1/2010 14:00
NULL                             1/4/2010 11:01          1/3/2010 11:00


so for the first row the date value from A should be returned, and in the 2nd row the date value from C should be returned.

Thanks in advance!
0
Aaron Goodwin
Asked:
Aaron Goodwin
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Shaun KlineLead Software EngineerCommented:
Does this example help?
declare @test table
(
  date1 datetime,
  date2 datetime,
  date3 datetime
)

insert into @test values ('1/1/2010 1:51', '1/1/2010 13:51', '1/1/2010 14:00')
insert into @test values (NULL, '1/4/2010 11:01', '1/3/2010 11:00')

select * from @test

select case when date1 < date2 then
            case when date1 < date3 then date1 else date3 end 
         else case when date2 < date3 then date2 else date3 end end
from @test

Open in new window

0
 
cyberkiwiCommented:
Create the reusable function first, then use it twice
create function min_dt(@a datetime, @b datetime) returns datetime
as
begin
return case when (@b is null) or (@b < 0) or @a < @b then @a else @b end
end
GO

declare @t table (a datetime, b datetime, c datetime)
insert @t values ('1/1/2010 1:51', '1/1/2010 13:51', '1/1/2010 14:00')
insert @t values (NULL, '1/4/2010 11:01', '1/3/2010 11:00')
insert @t values ('1/4/2010 11:01', NULL, '1/3/2010 11:00')
insert @t values ('1/1/2011 11:01', NULL, '1/3/2011 11:00')

select *, MIN_=dbo.min_dt(dbo.min_dt(a,b),c) from @t

Open in new window

0
 
cyberkiwiCommented:
Shaun,

Your query fails on the last one

insert @t values ('1/1/2011 11:01', NULL, '1/3/2011 11:00')
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Shaun KlineLead Software EngineerCommented:
True, it does fail. This correction does work:

select case when date2 is null or date1 < date2 then
        case when date3 is null or date1 < date3 then date1 else date3 end
      else case when date3 is null or date2 < date3 then date2 else date3 end end
from @test
0
 
awking00Commented:
select case when first_compare <= second_compare then first_compare
            else second_compare
       end as lowest_time_value from
(select case when a < b then a else b end as first_compare,
        case when b < c then b else c end as second_compare
 from tablex);
0
 
Aaron GoodwinAuthor Commented:
thank you very much!
0
 
cyberkiwiCommented:
I would have thought I at least assisted in the solution?
Also, are you opposed to functions which would be a reusable way of doing things...
0
 
Aaron GoodwinAuthor Commented:
Hi CyberKiwi,

Sorry, I did not mean to offend you.
The answer to you question is Yes, I am not comfortable with calling functions at my level of understanding.  Just straight forward SQL queries are what I like to work with.  I appreciate that you caught the error in the first round of code from Shaun.....but ultimately Shaun's principle idea, and provided the working solution, hence my assignment of the points.
0
 
cyberkiwiCommented:
I could have let that error go undetected, but I assisted didn't I?
Anyway, I'm off.
Life is too short.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now