简体   繁体   中英

CASE when is null in SQL Server

I am using a SQL Server database.

This query:

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

gives me en error:

[Error Code: 102, SQL State: S0001] Incorrect syntax near '='.

Any help is appreciated,

Thanks in advance

mismas

I think this is what you want.

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

SQL Server doesn't really have a boolean type that you can use as a result.

Try this code:

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

You could consider the coalesce keyword.

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

Coalesce returns the first argument that is not null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM