Oracle SQL - Fetch all rows if one among multiple columns not null - sql

Need to check OR condition between multiple columns.
select * from tableA where (COL1 or Col2) is not null
how to fetch this?
My actual query is something like :
select b.empname,b.hours,b.minutes from table1 a,table2 b
where a.pk_id=b.fk_id and a.type='UT'
and a.SOME_NUMBER='123' and b.hours is not null or b.minutes is not null;
but
b.hours is not null or b.minutes is not null;
is fetching repeated records

select b.empname,b.hours,b.minutes from table1 a,table2 b
where a.pk_id=b.fk_id and a.type='UT'
and a.SOME_NUMBER='123' and COALESCE(b.hours,b.minutes) is not null;
worked fine for me.
Thanks everyone for the input

Just use
select * from tableA where COL1 is not null or Col2 is not null

Its just select * from tableA where COL1 IS NOT NULL or Col2 IS NOT NULL.

Related

default value if a column doesnt exist in the SQL table

I have the below sample code. Assuming I do not know if a particular column exists in a table or not, how could I write the query in a way that I can default the column value to 'Not available' if the column doesn't exist in the table?
Example:
select COL1, COL2,
CASE
WHEN OBJECT_ID('COL3') IS NULL THEN 'Not Available'
ELSE COL3
END AS COL3
from TABLE1
Thanks in advance.
This is quite tricky to do (without dynamic SQL), but there is a way by playing with the scoping rules in SQL. You can do this assuming you have a unique or primary key in the table:
select t1.col1, t1.col2,
(select col3 -- no alias!
from table1 tt1
where tt1.id = t1.id -- the primary/unique key
) col3
from table1 t1 cross join
(values ('Not Available')) v(col3) -- same name
The subquery will fetch col3 from the table1 in the subquery if it exists. Otherwise it will reach out and find col3 from the values() clause.

sql replace null value with value with value in other column

I want to replace all values which has a NULL value with the the value in another column on the same row.
I have tried different sql statements without any success.
Regards
If you want to update, do something like this:
update t set col1 = col2
where col1 is null
If you just want to do selection, use most databases support coalesce:
select coalesce(col1, col2) from t;
If you want to Update the value from same column
UPDATE A
SET A.Col1 = A.Col2
From TableA A
Where A.Col1 IS NULL
If you want to update the value from Other table then Try this:
Update A
SET A.Column1 = B.Column1
From TableA A
INNER JOIN TableB B
ON A.SomeID = B.SameID
WHERE A.Column1 IS NULL
You can update the value by using join and update query.

using sql create a new table with 2 fields, field1 from tableA and field1 from table B

Am new to SQL and am stuck here with a very simple-looking query request.
I have 2 tables, both having exactly the same structure (IE same no. of columns, same no. Of rows) except for the actual contents. so for example,tableA has 2 columns called col1&col2; tableB has 2 columns too called col1&col2. Now I want to create a 3rd new tale, where 1st column is tableA's col1, and 2nd column is tableB's col1. preferably the name of the 1st column is fromTableA, and name of 2nd column is fromTableC. How do I achieve this please? I tried all the following ways but I always get the same error: "number of query values and destination fields are not the same."
variation 1:
insert into newTable(fromTable1,fromTable2)
select col1 from table1
select col1 from table2
variation 2:
insert into newTable(fromTable1,fromTable2)
select col1 from table1,col1 from table2
variation 3:
insert into newTable(fromTable1,fromTable2)
select col1 from table1, table2
Presumably you have fields in the two tables that can be joined, so this:
insert into newtable (romTable1,fromTable2)
select a.col1, b.col1
from table1 a, table2 b
where a.col1 = b.col1;
The a/b are aliases that differentiate between the two columns in each table. If you don't have fields to join then whatever you're trying to do probably needs a rethink.
You may try following sql query to achieve your purpose:
with OrderedTableA as (
select row_number() over (order by Col1) RowNum, *
from TableA (nolock)
),
OrderedTableB as (
select row_number() over (order by Col1) RowNum, *
from TableB (nolock)
)
select T1.Col1, T2.Col2 into TableC
from OrderedTableA T1
full outer join OrderedTableB T2 on T1.RowNum = T2.RowNum
Above query will create a new table as TableC with column col1 from TableA and col2 from TableB. You may change the queries to your need.
I hope you will understand the above queries. Give it a try.

Why doesn't the [NOT IN ] syntax work?

I try to execute the following query but it doesn't get any data although it should get one row :
select * from [DB1.table1] where col1 not in (select col2 from DB2.table2)
col1,col2 are of type varchar
why it doesn't work ?
"Doesn't work" isn't exactly a good description of your problem, but in nearly all cases this is caused by the sub-select returning NULL values.
You probably want this:
select * from [DB1.table1]
where col1 not in (select col2 from DB2.table2 where col2 is not null);
The comparison with NULL always yield "undefined" and thus if at least one row from the sub-select contains a NULL in the col2 column the whole expression is "undefined". As undefined not "true", the whole query doesn't return anything.
If you have NULLs in col2 in table2, you'll get the behaviour you describe:
create table table2 (
col2 varchar(10) null
)
insert into table2 (col2) values ('abc'),(null)
create table table1 (
col1 varchar(10) null
)
insert into table1 (col1) values ('abc'),('def')
select * from table1 where col1 not in (select col2 from table2)
Produces no rows. This is because the result of NOT IN becomes UNKNOWN once a NULL comparison occurs.
You can fix it with:
select * from table1 where col1 not in (select col2 from table2 where col2 is not null)
If that's the correct logic for your situtation.
As others have already pointed to the reason that cause this issue, you can achieve the same results, using LEFT JOIN and it safe than the predicate IN with the NULL vlaues:
select t1.*
from [DB1.table1] AS T1
LEFT JOIN DB2.table2 AS t2 ON t1.col1 = t2.col2
where t1.col2 IS NULL;

How to select all rows from tableA where two correlating columns in tableB are identical to the ones in tableA

Here is an example row found in both tableA and TableB:
col1 col2 col3
123 | asdf | ddd
I was going to try to use a CTE To retrieve this value, but i have only got it to work with one column at a time.
Here is what i have so far, but right now I am stumped.
;with cte as (
SELECT A.col1
,A.col2
FROM tableA A
)
select col1, col2 from cte where col1, col2
not in (select col1, col2 from FHU.dbo.HolidayCallers)
And just to reiterate i am expecting output to be the original sample row up top.
Based on your question, you'd could use EXISTS to find rows where the related columns are in both tables.
SELECT a.ConversationID, a.SendDateUtc
FROM tableA a
WHERE EXISTS(SELECT NULL
FROM FHU.HolidayCallers hc
WHERE a.ConversationID = hc.ConversationID
AND a.SendDateUtc = hc.SendDateUtc);
BUT, based on the sample code you provided, it seems like you are looking for a NOT EXISTS condition:
SELECT a.ConversationID, a.SendDateUtc
FROM tableA a
WHERE NOT EXISTS(SELECT NULL
FROM FHU.HolidayCallers hc
WHERE a.ConversationID = hc.ConversationID
AND a.SendDateUtc = hc.SendDateUtc);
You can use an INNER JOIN to find records that are in both tables:
SELECT a.ConversationID, a.SendDateUtc
FROM tableA AS a
INNER JOIN FHU.dbo.HolicayCallers AS hc ON a.ConversationId = hc.ConversationId
AND a.SendDateUtc = hc.SendDateUtc