In SQL/Transact SQL, I am trying to update a temp table to take the latest date from 3 different date columns (in that temp table), and put that MAX date into a "latest date" column.
What is the best way to do this, using an update statement?
In SQL/Transact SQL, I am trying to update a temp table to take the latest date from 3 different date columns (in that temp table), and put that MAX date into a "latest date" column.
What is the best way to do this, using an update statement?
Perhaps a union?
select max(myDate)
from (
select field_1 myDate from myTable where ...
union all
select field_2 myDate from myTable where ...
union all
select field_3 myDate from myTable where ...
) d
Of course, this hits the table three times for the same row. A CTE would probably solve that:
with myRow as (
select field_1, field_2, field_3 from myTable where ...
)
select max(myDate)
from (
select field_1 myDate from myRow
union all
select field_2 myDate from myRow
union all
select field_3 myDate from myRow
) d
Oracle has the GREATEST statement, you can sort of simulate it with functions or case statements
See this question:
Is there a Max function in SQL Server that takes two values like Math.Max in .NET?