如果 UpdatedDate 为 NULL,则派生列中的 Null 条件切换到 createdDate

Null condition in derived column to switch to createdDate if UpdatedDate is NULL

我有一名 table 员工,其中包含:

这是一个例子:

EmployeeId createdDate updatedDate
54         2013-07-10  NULL
245        2016-06-29  2016-07-03

如果updatedDate 为NULL,我们将只考虑createdDate。我正在使用派生列,如何使用条件?

我的查询如下:

SELECT IIF(ISNULL(updatedDate),createdDate , updatedDate)
FROM employee

在您的派生列中,条件必须如下所示:

ISNULL(UpdatedDate)  ? CreatedDate :  UpdatedDate

我只会使用 coalesce()。它完全符合您的要求, 它是标准的 SQL(不像 ISNULL(),它在某种程度上是特定于供应商的):

select coalesce(updatedDate, createdDate) from employee

ISNULL() 比较合适 :

SELECT ISNULL(updatedDate, createdDate)
FROM employee e;

ISNULL() 接受两个参数,一个是可能的 null 值,另一个是 null 值的替换。