Is null > 0 (integer) [duplicate] - sql

This question already has answers here:
What is NULL in SQL?
(5 answers)
Closed 8 years ago.
If I have in my sql something like that:
SELECT *
FROM SOMETABLE
WHERE ((SELECT ONECOLUMN FROM ANOTHERTABLE WHERE ID = 42) > 0)
If I got a NULL in ONECOLUMN it will be greater then 0?

As NULL means "not known", NULL is not greater than 0. It's not smaller either. It's not known. Hence NULL > 0 results in NULL, rather than in TRUE or FALSE.

NULL is not greater than zero.
NULL is not equal to zero.
NULL is not less than zero.
Any given integer you might choose would meet exactly one of these three conditions. But not NULL.
NULL isn't an integer. It's a marker indicating that a value is not present. This could mean that a value exists, but is unknown. It could also indicate that no value exists in this context.
You can use the ISNULL function to find out whether a NULL is present instead of a value. But if you compare a value with zero, and there is a NULL in place of the value, you won't get either TRUE or FALSE as a result.
If you are confused, you're in good company.

How about checking for NULL and then returning 0 if true otherwise return the value of ONECOLUMN:
SELECT *
FROM SOMETABLE
WHERE (
SELECT CASE WHEN ISNULL(ONECOLUMN) THEN 0 ELSE ONECOLUMN END
FROM ANOTHERTABLE
WHERE ID = 42
) > 0

Null is unknown and it cannot be compared to any value. It cannot be greater or lesser.

SELECT *
FROM SOMETABLE
WHERE (
IsNull((SELECT ONECOLUMN
FROM ANOTHERTABLE
WHERE ID = 42), IntValue) > 0
)
If you need to accept the Null so IntValue can be 1 otherwise -1 is OK.

Related

SQL order of commands

I have an issue where I'm looking in BRKRMASTID where I only want NULL values. And when I have a null value I want BRKR_FEE_PCT OR BRKR_FEE_AMT to not be 0. I have typed my code and for some reason I am still getting BRKRMASTID with values.
How can I make SQL only return values that are NULL and still have either BRKR_FEE_PCT or BRKR_FEE_AMT that are not equal to 0.
select *
from dbo.BRKRREF
where BRKRMASTID is null
or BRKR_FEE_PCT <> 0
or BRKR_FEE_AMT <> 0
Thanks for those who helped me so quickly. This edit is for a part 2 of the same issue. How can I get values where BRKMASTID is Null or = '0' with the same criteria for BRKR_FEE_PCT and BRKR_FEE_AMT?
Just like your question states, "How can I make SQL only return values that are NULL AND still have either BRKR_FEE_PCT or BRKR_FEE_AMT that are not equal to 0."
select *
from dbo.BRKRREF
where BRKRMASTID is null
AND (BRKR_FEE_PCT <> 0
or BRKR_FEE_AMT <> 0)
select *
from dbo.BRKRREF
where BRKRMASTID is null
and (BRKR_FEE_PCT <> 0 or
BRKR_FEE_AMT <> 0)

"!="/NOT perhaps not working properly in SQLite

I have a table with about a hundred rows. It has a column is_gallery that contains either 1, 0, or NULL. If I do...
SELECT * WHERE is_gallery != 1
or
SELECT * WHERE NOT (is_gallery = 1)
it excludes the rows where is_gallery is null. I can manage to get a proper response if I do
SELECT * WHERE (is_gallery = 0 OR is_gallery is null)
But shouldn't the "!=" or NOT work? Isn't there a way to just return the rows where is_gallery doesn't equal 1 without testing for every other possibility?
You can use the IS and IS NOT operators instead of = and !=. These treat NULL like a normal value.
SELECT * FROM yourTable WHERE is_gallery IS NOT 1
The best thing to use is coalesce as in:
SELECT *
WHERE coalesce(is_gallery,0) != 1;
what coalesce does, is replaces any null value in that column with the second parameter. In the example above, any nulls in the "is_gallery" column will be replaced with 0 before it is compared with 1. So will of course return true.
On NULL realize that a NULL value isn't equal to ANYTHING - not even NULL itself. It cannot be compared - so when "comparing", it always will return FALSE. On NULL, it has a special operator which is "IS NULL" or "IS NOT NULL"

divisor is equal to zero in sql need to add some sort of try catch

I have this query that i get errors when data gets loaded to this table and the divisor is zero which is the b. living_units column. I want to know if I can do some sort of try catch or if its zero to show null instead of failing or something that will help not error out?
SELECT a.SERVICE_TYPE_GRP,
a.HSIA_TYPE,
a.STAT_DYN_IND,
a.VIDEO_IND,
a.VOICE_IND,
a.CUST_CNT,
b.LIVING_UNIT_CNT,
a.DT_MODIFIED,
a.CUST_CNT / b.LIVING_UNIT_CNT AS ALLRGN_TK_RT_PCT
FROM ( SELECT lp.SERVICE_TYPE_GRP SERVICE_TYPE_GRP,
lp.HSIA_TYPE HSIA_TYPE,
lp.STAT_DYN_IND STAT_DYN_IND,
lp.VIDEO_IND VIDEO_IND,
lp.VOICE_IND VOICE_IND,
lp.DT_MODIFIED DT_MODIFIED,
SUM (lp.CUST_CNT) CUST_CNT
FROM RPT_SUBSCR_REGION_DTL lp
GROUP BY SERVICE_TYPE_GRP,
HSIA_TYPE,
STAT_DYN_IND,
VIDEO_IND,
VOICE_IND,
DT_MODIFIED) a,
( SELECT DT_MODIFIED, SUM (LIVING_UNIT_CNT) LIVING_UNIT_CNT
FROM RPT_REGION_CUST_DTL
WHERE dt_modified = (SELECT dt_modified
FROM ls_dt_modified
WHERE NAME = 'RPT_REGION_CUST_DTL')
GROUP BY DT_MODIFIED) b
WHERE a.DT_MODIFIED = b.DT_MODIFIED;
The error i get is ORA-01476: divisor is equal to zero.
If you have a complex divisor expression and a NULL result is acceptable, there is an alternative that does not require repeating the divisor expression:
a.CUST_CNT / nullif(b.LIVING_UNIT_CNT, 0)
The NULLIF function returns NULL if the first parameter is equal to the second parameter. In this case it returns NULL if b.LIVING_UNIT_CNT is equal to zero. And anything divided by NULL also becomes NULL. It's a bit of a "trick" maybe, but it saves the expression repetition of the CASE statement.
Try a case statement:
case
when b.LIVING_UNIT_CNT = 0 -- the divisor
then 0 -- a default value
else a.CUST_CNT / b.LIVING_UNIT_CNT
end
ALLRGN_TK_RT_PCT

Comparing 2 null datetimes not returning equal

In an update trigger I am comparing 2 nullable datetimes, that both happen to be null in my current test and it is returning that they are not equal.
#ExpirationDateChanged = case when i.ExpirationDate = d.ExpirationDate then 0 else 1 end
In this case i.ExpirationDate and d.Expiration date are null but instead of getting the expected 0, I am getting 1.
Can anyone explain this behavior?
NULL is not equal to NULL using equality comparison.
Comparing NULL values for equality is UNKNOWN under ANSI setting (the default)
You need to add an extra condition to explicitly test both columns for NULL using IS NULL:
#ExpirationDateChanged = case
when (i.ExpirationDate IS NULL AND d.ExpirationDate IS NULL) OR
i.ExpirationDate = d.ExpirationDate then 0
else 1
end

How does 'in' clause works in oracle

select 'true' from dual where 1 not in (null,1);
when we execute this which will result nothing
what my question is:
is the above query is logically equivalent to
select 'true' from dual where 1 != null and 1 != 1;
which will result nothing just as above statement
Please clarify?
Correct (but note that IN is an operator, not a clause and it works like this in SQL in general, not only for Oracle).
where 1 not in (null,1)
is equivalent to:
where 1 != null and 1 != 1
which should really be written as:
WHERE 1 NOT IN (NULL, 1)
and
WHERE 1 <> NULL AND 1 <> 1
which is the same as:
WHERE (1 <> NULL) AND (1 <> 1)
which evaluates to:
WHERE UNKNOWN AND FALSE
and further as:
WHERE FALSE
So, it correctly returns no rows.
Notice that if you had WHERE 1 NOT IN (NULL, 2), it would evaluate to WHERE UNKNOWN (left as an exercise) and no rows would be returned either.
The issue of your script in comparing with NULL value. You should use
column is null and column = 1
Actually NULL is an undefined value. Any comparation with NULL gives neither True nor False but NULL. Even NULL = NULL
That's why your 1 not in (null,1) doesn't work.
Yes they are.
select something from table where column not in (1,2,3);
is equivalent to
select something from table where column != 1 and column != 2 and column != 3;
The IN statement is a collection of OR statements, while NOT IN is a collection of AND statements - but it is also not equal to.
So the NOT IN is equivalent to:
1 <> NULL
AND 1 <> 1
AND ...
While the IN would be equivalent to:
1 = NULL
OR 1 = 1
OR ...
Note that having NULL in the collection will not work, due to the quirky nature of NULL.
Yes. It is correct. Also NULL values should be compared with IS NULL