Reputation: 65
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 = ?
Upvotes: 2
Views: 1317
Reputation: 65
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
Upvotes: 0
Reputation: 2956
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<>'');
Upvotes: 0
Reputation: 311843
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
Upvotes: 0
Reputation: 10212
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';
}
Upvotes: 1