选择不为null

时间:2018-01-22 12:22:22

标签: sql select sql-server-express

在SQL查询中,我想选择一个可空列作为布尔值,具体取决于它是否为null。简单的例子:

SELECT name AS UserName, age AS UserAge, (birthday IS NOT NULL) AS HasEnteredBirthday
FROM users

结果:

Incorrect syntax near the keyword 'IS'.

预期结果:

Peter 31 1
Greg  54 0

1 个答案:

答案 0 :(得分:1)

并非所有数据库都支持布尔类型。您可以将case用作:

SELECT name AS UserName, age AS UserAge,
       (CASE WHEN birthday IS NOT NULL THEN 'true' ELSE 'false' END) AS HasEnteredBirthday
FROM users;

通常,整数“1”用于true,“0”用于false。