我有一个列(类型为Date
),该列为null或非null。我想创建一个列,根据列是否为null,显示“已完成”或“未完成”。我环顾四周,找到IsNull()
,但这只允许我选择一个结果。
我想象下面的SQL语句,但无法找到所需的语法。
select rowID, if(isNull(CompletedDate), "Yes", "No")
From Table
示例数据
| RowID | CompletedDate |
| 1 | 2015-12-01 |
| 2 | |
| 3 | 2015-12-01 |
预期结果
| RowID | Completed|
| 1 | Yes |
| 2 | No |
| 3 | Yes |
这是使用SQL Server
完成的答案 0 :(得分:2)
你可以这样写:
select
rowid,
case when completeddate is null then 'No' else 'Yes' end as Completed
from tablename
答案 1 :(得分:1)
尝试使用CASE
https://msdn.microsoft.com/es-es/library/ms181765(v=sql.120).aspx
select rowID, CASE WHEN CompletedDate IS NULL THEN 'No'
ELSE 'Yes'
END as Completed
From Table
答案 2 :(得分:1)
在SELECT
陈述中:
IF C THEN R = CASE WHEN C THEN R END
IF C THEN R1 ELSE R2 = CASE WHEN C THEN R1 ELSE R2 END
IF C1 THEN R1 ELSE IF C2 THEN R2... = CASE WHEN C1 THEN R1 WHEN C2 THEN R2... END
答案 3 :(得分:1)
select rowID, if(isNull(CompletedDate), "Yes", "No")
From Table
它是MySql语法if(isNull(CompletedDate), "Yes", "No")
,但在SQL Server中,您可以使用CASE
或IIF
条件来实现。
使用CASE:
select
rowID,
CASE WHEN CompletedDate IS NULL THEN 'No'
ELSE 'Yes'
END as Completed
IIF
条件:
select
rowID,
IIF(CompletedDate IS NULL, 'No', 'Yes') as Completed