Select 不为空

Select not null as

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

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

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

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

通常,整数“1”代表真,“0”代表假。