sql - get the latest date of two columns

Posted by stacker on Stack Overflow See other posts from Stack Overflow or by stacker
Published on 2010-05-27T00:36:10Z Indexed on 2010/05/27 0:41 UTC
Read the original article Hit count: 224

Filed under:
|
|
|
table1
    - date1 datetime not null
    - date2 nvarchar null

I want to get the latest date of this two.

select date1, date2, 
(CASE WHEN date1 > CAST(date2 as DateTime) THEN date1 ELSE date2 END) as DateTime) as LatestDate
from table1

please note that date2 can be null. in this case, date1 win.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008