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!
Aaron GoodwinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.