table1
- date1 datetime not null
- date2 nvarchar null
我想知道这两个人的最新日期。
select date1, date2,
(CASE WHEN date1 > CAST(date2 as DateTime) THEN date1 ELSE date2 END) as DateTime) as LatestDate
from table1
请注意,date2可以是null
。在这种情况下,date1 win。
答案 0 :(得分:1)
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
答案 1 :(得分:1)
将CAST(date2 as DateTime)
更改为CAST(isnull(date2,'01/01/1900') as DateTime)
答案 2 :(得分:0)