Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 887
mismas Avatar asked Mar 22 '23 21:03

mismas


2 Answers

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.

like image 83
Laurence Avatar answered Apr 06 '23 05:04

Laurence


Try this code:

select * 
from table1 
where table1.name='something' 
and (table1.date is null Or table1.date >= '2013-09-24');
like image 33
Amir Keshavarz Avatar answered Apr 06 '23 05:04

Amir Keshavarz