SIn san sun
SIn san sun

Reputation: 613

Not return row from table when use the case when condition

I have a basic question. I want to return data from my table when the condition is true but when it is not I got the null row, but I don't want that.

This is my example:

 select case when coalesce(var, '') != '' then (select id from user) end

when var is not '' I got data, bat, when is not I, got null

enter image description here

Upvotes: 0

Views: 1187

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269445

You seem to be asking for:

select id
from "user"
where var <> '';

You don't need to handle NULLs as a special case, because <> will not return any rows when var is NULL.

Upvotes: 0

gbeaven
gbeaven

Reputation: 1790

I think you simply just need to filter out your NULLs returned in a where clause:

select coalesce(var, id)
from user
where coalesce(var, id) is not null

coalesce returns the first non-null value in a list of fields. When neither condition is met a NULL row will be returned and the where clause will filter those out.

Upvotes: 1

Laurenz Albe
Laurenz Albe

Reputation: 246033

Filter with a WHERE condition:

select id from "user"
where coalesce(var, '') = '';

Upvotes: 1

Related Questions