根据行是否为空来选择值

时间:2018-07-19 18:30:03

标签: sql sql-server tsql

我有一个存储过程,我想根据其中是否为空来决定是选择一列还是其他列。所以我有两列

CreationDateModifiedDate

我的选择是这样的:

SELECT
            [P].[LegacyKey] AS [Job No]
           ,[P].[Name] AS [Proj Name]

            FROM Project AS [P]

仅当CreationDate等于ModifiedDate,并且NULL不为空时,我才选择ModifiedDate。我该如何实现?问候

2 个答案:

答案 0 :(得分:2)

您似乎想要coalesce()

SELECT [P].[LegacyKey] AS [Job No], [P].[Name] AS [Proj Name],
        COALESCE([P].ModifiedDate, [P].CreationDate)
FROM Project AS [P];

答案 1 :(得分:1)

Isnull()也将起作用:

SELECT
[P].[LegacyKey] AS [Job No],
[P].[Name] AS [Proj Name],
ISNULL([P].ModifiedDate, [P].CreationDate)

FROM Project AS [P]