CASE when 在 SQL Server 中为空



我正在使用SQL Server数据库。

此查询:

SELECT *
FROM   table1
WHERE  table1.name = 'something'
       AND CASE
             WHEN table1.date IS NULL THEN 1 = 1
             ELSE table1.date >= '2013-09-24'
           END; 

给我 en 错误:

[错误代码:102,SQL 状态:S0001] "="附近的语法不正确。

任何帮助不胜感激,

提前致谢

米斯马斯

我认为这就是你想要的。

select
    * 
from 
    table1 
where 
    table1.name = 'something' and (
        table1.date is null or 
        table1.date >= '2013-09-24'
    );

SQL Server 实际上没有可用于此目的的布尔类型。

试试这段代码:

select * 
from table1 
where table1.name='something' 
and (table1.date is null Or table1.date >= '2013-09-24');

你可以考虑合并关键字。

select
    *
from 
    table1
where 
    table1.name='something' and 
    coalesce(table1.date,'2013-09-24') >= '2013-09-24';

合并返回第一个不为 null 的参数。

相关内容

最新更新