gpt4 book ai didi

sql - 从三列中获取最新日期

转载 作者:行者123 更新时间:2023-12-02 10:42:25 26 4
gpt4 key购买 nike

ID     Date1           Date2            Date3   
158 5/3/13 15:11 2/20/13 11:38 2/20/13 11:38

我想从这三列中获取最新日期。

最佳答案

SELECT CASE WHEN Date1 IS NOT NULL 
AND Date1>=COALESCE(Date2,CAST('0001-01-01 00:00' AS DATETIME2))
AND Date1>=COALESCE(Date3,CAST('0001-01-01 00:00' AS DATETIME2)) THEN Date1
WHEN Date2 IS NOT NULL
AND Date2>=COALESCE(Date1,CAST('0001-01-01 00:00' AS DATETIME2))
AND Date2>=COALESCE(Date3,CAST('0001-01-01 00:00' AS DATETIME2)) THEN Date2
WHEN Date3 IS NOT NULL
AND Date3>=COALESCE(Date1,CAST('0001-01-01 00:00' AS DATETIME2))
AND Date3>=COALESCE(Date2,CAST('0001-01-01 00:00' AS DATETIME2)) THEN Date3
END AS latest
FROM t1

Example

关于sql - 从三列中获取最新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23864172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com