Get min date from column values

I have a table with 10 datetime columns/fields.

How do I get the min date of all the fields.

DATE1, DATE2, DATE3, DATE4, DATE5....


What is the SQL to get the MIN date from columns in the same record?


Need the earliest date of any of the 10 datetime columns.

thx
LVL 1
JElsterAsked:
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.

arnoldCommented:
You have to unpivot the table
Converting
Column, column1, column2, column3
Into
Heading1,heading2
Column,column1
Column,column2
Column,column3

https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
Vitor MontalvãoMSSQL Senior EngineerCommented:
Something like this:
WITH CTE_DATES (ID, Dates)
AS (
    SELECT ID, DATE1
    FROM TABLE
    UNION ALL
    SELECT ID, DATE2
    FROM TABLE
    UNION ALL
    SELECT ID, DATE3
    FROM TABLE
    UNION ALL
(...)
    SELECT ID, DATE10
    FROM TABLE
)
SELECT ID, MIN(Dates)
FROM CTE_DATES
GROUP BY ID

Open in new window

JElsterAuthor Commented:
This is sql server 2005

Incorrect syntax near the keyword 'WITH'.
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Then use this version:
SELECT ID, MIN(Dates)
FROM  (SELECT ID, DATE1 AS Dates
    FROM TABLE
    UNION ALL
    SELECT ID, DATE2
    FROM TABLE
    UNION ALL
    SELECT ID, DATE3
    FROM TABLE
    UNION ALL
(...)
    SELECT ID, DATE10
    FROM TABLE) AS T
GROUP BY ID

Open in new window

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
arnoldCommented:
IMHO, depending on how many date Columbus you have, unpivot is better than trying to union all for the columns.
JElsterAuthor Commented:
just needed a fast simple solution.. don't have time now to research pivot
thx
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.