>I have a SQL table with dates in 3 columns. I'm writing a query and I want to get the earliest date from the 3 columns as 'StartDate', and the latest of the dates in the 3 columns as 'EndDate'.
>
>Is there the SQL equivalent to the VFP Min() and MAX() function?
>
>Example:
>
>select min(DateCol1, DateCol2, DateCol3) as StartDate, max(DateCol1, DateCol2, DateCol3) as EndDate from ...
>
>I've been looking, but can't find anything.
>
>Thanks,
>Larry
You can use case expression or UNPIVOT (SQL Server 2005 and up), e.g.
select Min(DateCol) as StartDate, max(DateCol) as EndDate from
(select DateCol1, DateCol2, DateCol3 from myTable
UNPIVOT (DateCol for DateType in ([DateCol1],[DateCol2],[DateCol3])) unpvt) X
If it's not broken, fix it until it is.
My Blog