开发者

sql - get the latest date of two columns

table1
    - date1 datetime not null
    - date2 nvarchar null

I want to get the latest date of this开发者_JAVA技巧 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.


SELECT date1, date2,  
CASE 
  WHEN date1 > CAST(ISNULL(date2,'1900-01-01') as DateTime) THEN 
     date1 
  ELSE 
    CAST(date2 as DateTime) 
END  as LatestDate 
FROM table1


change CAST(date2 as DateTime) to CAST(isnull(date2,'01/01/1900') as DateTime)


This is very nice solution, but I suspect Nathan's solution is better.

http://p2p.wrox.com/oracle/6592-calculate-de-max-value-two-columns.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜