CASE when null in SQL Server

I am using a SQL Server database.

This request:

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 an error:

[Error code: 102, SQL state: S0001] Invalid syntax next to '='.

Any help is appreciated

Thank you in advance

mismas

+4
source share
3 answers

Try this code:

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

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 really does not have a boolean type that you can use as a result.

+4
source

coalesce.

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

Coalesce , .

+1

All Articles