Oracle SQL: Where 2 columns are not null - sql

Should be simple enough I think but I'm not getting it and I'm not sure how to explain it without an example to look it up.
Table:
1. A B
2. null 1
3. null null
4. 1 null
5. 1 1
I want my query to omit anything where both column A and B are null (in example, row 3 would be omitted).
select * from Table where (A is not null) and (B is not null)
Is just giving me columns where both are present.
Any help would be great.
*edit: changed "return" to "omit"

You can filter like this:
select * from Table where nvl(A,B) is not null

Related

SQL to join two tables with one where clause on one table

I have two tables as the following
table1
main_id main_val main_sub main_pk
1 A NULL 3
3 A 1 3
table2
col_id col_val
1 A
select table1.main_pk from table1 ,table2 WHERE
table1.main_id = table2.col_id
and table1.main_val = table2.col_val
and table1.main_sub = null
Am expecting the above query to select the first row in table 1 as main_sub is null and the other two columns matches. But it does not. I am just learning SQL basics so am not sure where am going wrong. Please help
I didn't fully understand the question but I think this is what you want:
SELECT * from table1 INNER JOIN table2 on table1.main_val=table2.main_val WHERE table1.main_sub IS NULL
If not I think it directs you in the right path
SQL is a little weird about NULL values. Use the condition
....
table1.main_sub IS NULL
for testing for a null specifically. The idea is that NULL is supposed to be interpreted as nothing, so nothing -equals- nothing can't ever be true because you can't compare something that doesn't exist.

Select from table with 2 condition and 2 operations

I have a Table BoxTrans
the table Contain Rows (ID,Date,FromBox,ToBox,Value)
I want to make a View like (ID,Date,Box,ValueIn,ValueOut)
select when frombox Give Value to ValueOut
and when tobox Give Value to ValueIN
You can use a CASE statement to check the value of a different column when populating a column. The below query will return your output as long as either ToBox or FromBox is NULL, if they are both not null you may get unexpected results.
SELECT ID,
Date,
COALESCE(ToBox,FromBox) as Box,
CASE WHEN ToBox IS NOT NULL THEN value ELSE NULL as ValueIn,
CASE WHEN FromBox IS NOT NULL THEN value ELSE NULL as ValueOut
FROM BoxTrans

<> and != avoiding NULL in WHERE condition

I have a table PATIENT with Column STATUS. When I queried to get STATUS not equal to 1, I was expecting the result as NULL and 2.
But I am only getting 2 as the result. Can someone help me with this?
CREATE TABLE #PATIENT
(STATUS INT)
INSERT INTO #PATIENT (STATUS)
SELECT 1
UNION
SELECT 2
UNION
SELECT NULL
SELECT * FROM #PATIENT WHERE STATUS <> 1
When I queried with
SELECT * FROM #PATIENT WHERE ISNULL(STATUS, 0) != 1
I am able to get NULL and 2 as the result.
This is SQL SERVER 2012.
You can use OR in WHERE with Condition STATUS IS NULL .
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
This will do it.
EDIT:
Conceptually, NULL means “a missing unknown value” and it is
treated somewhat differently from other values.
You cannot use arithmetic comparison operators such as =, <, or <>
to test for NULL
Because the result of any arithmetic comparison with NULL is also
NULL, you cannot obtain any meaningful results from such comparisons
we can not equate or not equate anything with null, thats why IS NULL
SELECT NULL <> 1 ===> NULL
Even though it is supposed to be true, it will return `NULL`
Hope this helps.
When you compare NULL value to any value then the result is always NULL.
So if you wan to select the NULL value as well then try this:
SELECT * FROM #PATIENT WHERE STATUS <> 1 OR STATUS IS NULL
DEMO
Conceptually "NULL" means a missing value. To test for NULL IS NULL or IS NOT NULL condition is used. Arithmetic operators cannot be used for comparing NULL values.
NULL<>1 :: returns false because NULL can be 1 or any other value (an unknown value).
Null is a special marker used in Structured Query Language (SQL) to indicate that a data value does not exist in the database.
And reason because you got only 2 and NOT NULL is because in SQL nothing is equal to NULL but also nothing is NOT equal to NULL.
In Simple words you cannot equate and not equate any value with NULL.

Null value in column resulting into no output in sql query

I have 2 tables (stud and stud1). Both having 2 columns but stud1 contains 1 record which is null.
I have created following 2 queries.First one is returning the accurate result but other one which is using not in returning nothing. I guess that is because of the null value. But I don't understand the reason for it. Can someone help me with this?
See NOT IN clause and NULL values.
That's because your 2nd query equals:
SELECT * FROM #stud
WHERE ID <> NULL
When ansi_nulls is on, ID <> NULL is unknown, so you won't get any rows.

SQL: how to fill all empty code fields

i'm working with db2
so i have some table and the question is how to fill all empty code fields
raws in the table represent some real world hierarchy
so i need to put non empty value of the CODE field into empty CODE fields according to information in the field LINK
my table is like that
Objid link code
1 0 555
2 1
3 2
4 3
ideally i need to do this inside one CREATE TABLE ... AS SELECT operator to create an mqt which would be later automatically refreshed on the regular base
all i have created by now is
CASE
WHEN (code is NULL or code = '')
THEN (select code from some_other_table
where objid = link and code is not NULL
)
WHEN (code != '' and code is not NULL)
THEN code
ELSE NULL
END AS code,
i think it must be some kind of while loop which i can't put inside my CREATE TABLE AS SELECT
Are there any solutions without using procedures or functions?
I think what you might be looking for is a "select for update" type query. This can be done similar to the following:
UPDATE SOME_DB.SOME_TABLE A
SET CODE = (SELECT SOME_CODE_FIELD
FROM SOME_OTHER_DB.SOME_OTHER_TABLE B
WHERE A.KEY = B.KEY)
WHERE CODE IS NOT NULL
OR CODE = "";
This will update table A with the code value from table B, if it has one, for all rows on table A with a null code. If table B has no code value, I believe the subselect will return null (provided the code column in table B is nullable) and the value of that code on table A will remain null.
If you are doing a CREATE TABLE AS SELECT, can't you just join to the table that has the missing code?
SELECT
COALESCE(CASE WHEN code='' THEN null ELSE code END,so.code) as code
FROM CodeTable ct
LEFT JOIN SomeOtherCodeTable so ON
ct.objid = so.link
AND so.code IS NOT NULL