select c.status from Table c where c.id = 13513 and status != false;
true
true
But not get null value. I want to get this like value and haw to get?
true
true
null
I don't have much to go on with the context you gave, but try this:
select c.status from Table c where c.id = 13513 and (status != false or status is null);
If that doesn't work, please post your table schema and, preferably, a data representation that contains the "null" you're looking for.
Related
In my Postgres database, I'm checking user answers for correctness by checking if two IDs, "user_answered_id" and "expected_answer_id", are equivalent. If the user doesn't provide a "user_answered_id", then we still mark their answer as incorrect.
In Postgres, the following queries
select case when 1 != null then TRUE else FALSE end as test;
select case when 1 = null then TRUE else FALSE end as test;
both result in FALSE. This is true for any number check (e.g., when 2 != null, when 3 != null, ..., etc.
Why doesn't CASE WHEN show TRUE for 1 != null?
Must I put in the check "or is null"? E.g.,
CASE WHEN
user_answered_id != expected_answer_id
OR user_answered_id IS NULL
THEN TRUE
ELSE FALSE
END as user_incorrect_tally
What you are looking for is: IS DISTINCT FROM
select 2 is distinct from null;
?column?
----------
t
select 2 is distinct from 1;
?column?
----------
t
From the docs:
datatype IS DISTINCT FROM datatype → boolean
Not equal, treating null as a comparable value.
1 IS DISTINCT FROM NULL → t (rather than NULL)
NULL IS DISTINCT FROM NULL → f (rather than NULL)
SQL uses three-valued logic: true, false, and null. Null is not false. Null can be thought of as "no value".
Operations on null almost always yield null. So 1 != null is null. 1 = null is null. null = null is null. 5 < null is null. Etc.
To check for null, use is null and is not null.
Back to your query. is not distinct from and is distinct from are like = and != which treat null as a comparable value. So null is distinct from 1 will be true.
select
user_answered_id is distinct from expected_answer_id as user_incorrect
If you need to convert a null into a different value such as 0 or an empty string, use coalesce.
select
coalesce(user_answered_text, 'No Answer')
Your column is named "tally", but a tally means a count. If you intend to count a user's true and false answers use count with a filter.
select
count(user_answered_id) filter (
where user_answered_id = expected_answer_id
) as user_correct_tally,
-- count ignores null, this will only be the questions they tried to answer
count(user_answered_id) as user_answered_tally,
count(user_answered_id) filter (
where user_answered_is is distinct from expected_answer_id
) as user_incorrect_tally
Yes, You should check NULL value with is null, And last query you wrote is correct.
I suggest you to read below documents:
https://www.postgresql.org/docs/current/functions-comparison.html
I have the following query which after executing sets the arts variable to null.
In SQL Server Profiler I can see the query is well formed, is being executed and is returning rows (there's only one thing to notice: the query includes all columns of all the tables involved).
Why the results collection is set to null even when the SQL query is working?
And why does the SQL query include all the columns and not only the ones specified in the anonymous type?
Thanks a lot.
Dial.
var arts = from rp in ent.ReportesDePrecios
join arp in ent.ArtículosDeReporteDePrecios on rp.Id equals arp.ReporteDePreciosId
//join crp in ent.CategoríasDeReporteDePrecios on rp.Id equals crp.ReporteDePreciosId
join a in ent.Artículos on arp.ArtículoId equals a.Id
where a.CategoríaId != null
join p in ent.Precios on new { Precio = a.Id, rp.ListaDePreciosId } equals new { Precio = p.ArtículoId, p.ListaDePreciosId }
where p.Activo == true
select new
{
CategoríaId = a.CategoríaId.Value,
a.FabricanteId,
ArtículoId = a.Id,
a.Código,
Precio = p.Valor
};
Sorry, one table was empty. That was one of the problems. Thanks a lot.
I have a stored procedure which takes a #flag as a parameter. That flag supposes to indicate to select null values or none null values.
for none null values my solution looks like that:
#Flag int
SET #Flag = NULL
WHERE ISNULL(column1,'') = ISNULL(#Flag,'')
Is there a way to accommodate none null values in similar manner ? If no what would be the most compact solution ?
Flag int
SET #Flag = NULL
select * from table
WHERE (#flag is null and column is null) or ((#flag is not null and column is not null) and #flag = column)
I advise to NEVER SET ANSI_NULL OFF, NEVER! That can lead to a lot of unnecessary maintenance pain.
No need for a compact solution like
ISNULL(column1,'') = ISNULL(#Flag,'')
Also that ill return null rows when #flag = '' and also ill return '' rows when flag is null
The only way I can think of is SET ANSI_NULL OFF and then do comparison: column1 = #flag
Today I was surprised by this case behaviour:
select case when null then true else false end;
case
------
f
I would expect it to return null since a null casted to boolean yelds a null not a false:
select null::boolean is null;
?column?
----------
t
Any comments on the rationale of this behaviour? What Am I missing?
Use something like
case when p.parent_id is null then false else true end as has_parent
In case you (also) need something like
if column is null then 'value'
Use:
COALESCE(column_name, 'replacment for null value') as column_name
In case you still need a case statement then use:
case COALESCE(column_name, 'asdf')
when 'asdf' then true
else false
end as desired_column_name
You're thinking of the CASE expression like it was taking the null as input to a function or operator, where null input generally results in null output:
regress=> SELECT 't'::boolean = NULL::boolean;
bool
------
(1 row)
wheras in fact it behaves like a WHERE clause in terms of null handling:
craig=> SELECT 't' WHERE NULL;
?column?
----------
(0 rows)
In WHERE clauses - and in CASE, a NULL result from a test expression is treated as "not true and therefore false". In some ways it's regrettable that the SQL standard didn't make a NULL result in a WHERE expression an error instead of treating it as false, but that's how it is.
This is yet another painful symptom of NULL's split personality, where the SQL spec can't decide if NULL means "unknown/undefined value" or "the absence of a value", much like the horrible mess with NULLs and aggregates.
You can use 'select' to check DATA column for null:
select
ID,
case (select 1 where DATA is null)
when 1 then 'no data'
else data
end
from ...
As the PostgreSQL documentation states:
If no WHEN condition is true then the value of the case expression is
the result in the ELSE clause. If the ELSE clause is omitted and no condition matches, the result is null.
Postgresql does not cast the output, and since you have an else condition, you're getting false. The next query returns a null value (Since there's no else condition)
select case when null then true end;
The CASE statement you wrote has two branches:
The one, when null then true, will never happen (because null is not equal to true)
And else branch, that will happen when there are no matches in regular when branches.
Example:
CASE WHEN val = 1 THEN 5
WHEN val = 2 THEN 10
ELSE 20 /*all other values, including null*/
END
select case when null is null then null else 1 end
returns null
select case null when then null else 1 end
returns 1
It seems that in PostgreSQL, empty_field != 1 (or some other value) is FALSE. If this is true, can somebody tell me how to compare with empty fields?
I have following query, which translates to "select all posts in users group for which one hasn't voted yet:
SELECT p.id, p.body, p.author_id, p.created_at
FROM posts p
LEFT OUTER JOIN votes v ON v.post_id = p.id
WHERE p.group_id = 1
AND v.user_id != 1
and it outputs nothing, even though votes table is empty. Maybe there is something wrong with my query and not with the logic above?
Edit: it seems that changing v.user_id != 1, to v.user_id IS DISTINCT FROM 1, did the job.
From PostgreSQL docs:
For non-null inputs, IS DISTINCT FROM
is the same as the <> operator.
However, when both inputs are null it
will return false, and when just one
input is null it will return true.
If you want to return rows where v.user_id is NULL then you need to handle that specially. One way you can fix it is to write:
AND COALESCE(v.user_id, 0) != 1
Another option is:
AND (v.user_id != 1 OR v.user_id IS NULL)
Edit: spacemonkey is correct that in PostgreSQL you should use IS DISTINCT FROM here.
NULL is a unknown value so it can never equal something. Look into using the COALESCE function.