Reputation: 231
In a database, among the fields, I have two fields which are picName
and UserName
.
The picName
can have a NULL
value, a picture's name or it can be empty.
I want to query the userName
of those entries whose picName
is either NULL
or empty.
I tried these commands separately
Select userName from user where picName = ''
Select userName from user where picName IS NULL
But I want to get both the results in 1 query.
I tried using IN
operator like this
Select userName from user where picName IN ('' , IS NULL)
But it didn't work.
How should I do it...?
Upvotes: 6
Views: 33791
Reputation: 359
Solution that checks for both empty and NULL values.
COALESCE(NULLIF(table.column, ''), table.fallback)
So your query will be:
SELECT userName FROM user WHERE NULLIF(picName, '') IS NULL
Upvotes: 0
Reputation: 1
...ISNULL(picName ,'') is a function of MSSQL
select case when (picname is null or picname = '' then username else 'filled' end from user
gives you the UserName if picname = '' or null else the text 'Filled'. If you want to filter these ones use (like the other said): ...where picname is null or picname = ''
Upvotes: 0
Reputation: 125855
An alternative to the other answers is to use MySQL's IFNULL()
or COALESCE()
functions:
SELECT userName FROM user WHERE IFNULL(picName='',TRUE);
SELECT userName FROM user WHERE COALESCE(picName,'') = '';
Upvotes: 5
Reputation: 32953
You want one of both conditions to be satisfied, so condition one OR
condition two must be true, and that's exactly what you need to write:
Select userName from user where picName = '' OR picName IS NULL
Upvotes: 1
Reputation: 263703
use OR
if you have multiple filters in your condition
Select userName
from user
where picName IS NULL OR
picName = ''
Upvotes: 19