SQL order of commands - sql

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)

Related

Is null > 0 (integer) [duplicate]

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.

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

TSQL Comparing 2 uniqueidentifier values not working as expected

I'm trying to compare 2 uniqueidentifier values as shown in the query below. However, if one value is null and one value isn't, the result is 'same'?! I'm sure that both values are uniqueidentifiers, and have also tried casting both values to uniqueidentifier to make absolutely sure. The 2 values being compared are coming from different databases with different collations. Does the collation make any difference? Any ideas would be appreciated.
select [result] = case when
[target].StaffID <> [source].StaffID then 'different'
else 'same'
end
from
...
If I replace the <> with an = the query then thinks that 2 null values don't match.
EDIT:
I used:
declare #empty uniqueidentifier
set #empty = '00000000-0000-0000-0000-000000000000'
... isnull(somevalue, #emtpy) <> isnull(othervalue, #empty) ...
NULL is neither equal to something nor equal to nothing. Generally you'd check for null values by comparing with IS NULL. For example,
somefield IS NULL
You could look into using COALESCE for what you're trying to do -- just make sure you use the same data types (in this case UniqueIdentifier):
...
case
when coalesce(t.StaffID,'00000000-0000-0000-0000-000000000000') <>
coalesce(t2.StaffID,'00000000-0000-0000-0000-000000000000')
then 'different'
else 'same'
end
...
http://sqlfiddle.com/#!3/181e9d/1
null is more of an unknown, it's not really a value. Unfortunately SQL will tell you that null = null is false.
Basically you have to cast nulls to empty strings than you can compare. We use IFNULL(value, replacement) to do that...
http://msdn.microsoft.com/en-us/library/ms184325.aspx
Hope this helps.
select case when null = null then 'equal' else 'not equal' end
Above will be "not equal"
select case when ISNULL(null, '') = ISNULL(null, '') then 'equal' else 'not equal' end
This one will be "equal"
And finally your case...
select [result] = case when
ISNULL([target].StaffID, '') <> ISNULL([source].StaffID, '') then 'different'
else 'same'
end
from

CASE WHEN NULL makes wrong result in SQLite?

I have a table with a column of image type, the table has some rows but all the rows haven't had any image yet, they are all null. To test the CASE WHEN NULL, I've tried this and it gave a strange result:
SELECT CASE myImageColumn WHEN NULL THEN 0 ELSE 1 END FROM myTable
All the returned rows were in a column of 1's (I thought 0's). What is wrong here?
Your help would be highly appreciated!
Thank you!
You can't compare with NULL like that, you should try:
SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END
FROM myTable
Use a different form of CASE instead:
SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END FROM myTable
Two useful links:
http://www.sqlite.org/nulls.html
http://www.sqlite.org/lang_expr.html
There's a bypass:
CASE ifnull(myValue, 'someUniqueStringOrValue')
WHEN 'someUniqueStringOrValue' THEN 0 -- this means null
WHEN 'someNormalValue' THEN 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