Question

I am using sqlsrv driver with PHP 5.3.27. How can I pass "is null" as a parameter to a query. So instead

SELECT row_id FROM table WHERE name IS NULL

I could use

SELECT row_id FROM table WHERE name = ?
Was it helpful?

Solution 4

I though of another way to do that myself

SELECT row_id FROM table WHERE ISNULL(name,-1)=?

If you're sure that you will never use '-1' as a condition, this will work. Just use your parameter when you need a value and '-1' when you need "IS NULL". But I guess there is no way to pass "is null" as a parameter

OTHER TIPS

Well... What prevents you from writing an if/else clause?

if($i_need_a_value) {
    $query = 'SELECT row_id FROM table WHERE name = ?';
} else {
    $query = 'SELECT row_id FROM table WHERE name IS NULL';
}

You could use a case statement to work around the nullability issue. A neat trick is to use a join with a single-row dummy query to avoid having to bind your value twice:

SELECT row_id 
FROM   table
JOIN   (SELECT ? AS param) dummy
WHERE  (name IS NULL AND param IS NULL) OR name = param

u can check for the particular column if it is null or it contains somevalue by the following query.

select row_id 
from table 
where (name IS NULL) or (name IS NOT NULL && name<>'');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top