sql replace null value with value with value in other column - sql

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.

Related

How do I UPDATE from a SELECT of same table

I should update a column of table by converting other column of same table in oracle. something like this:
update table1 set colum1=(select TO_CHAR(column2) from table1)
but I have this error
single-row subquery returns more than one row
could I do this?
It's just
update table1 set
column1 = to_char(column2);
If you ever need it for more dynamic value generation, use WHERE to match a specific record
update table1 a
set colum1 = (select TO_CHAR(column2) from table1 b where b.id = a.id)
where a.id = .... -- <-- optional, for specific records

Merge multiple tables into one table SQL Server

I want to merge 3 tables into one only to get full data with conditions below
merge 3 tables [test1], [test2], [test3] into one table [test]
If when merging, if a column is blank select from another table
Col1 exists in all 3 tables
Here is my example code to merge from table 2 into test.
MERGE INTO [dbo].[test] a
USING [dbo].[test2] b ON a.col1 = b.col1
WHEN MATCHED THEN
UPDATE
SET col1 = b.col1,
col2 = b.col2,
col3 = b.col3,
col4 = b.col4
WHERE col1 = '' OR col2 = '' OR col3 = '' OR col4 = '';
It get errors:
Incorrect syntax near the keyword 'where'.
Please try the COALESCE function rather than using the where condition. The COALESCE function takes the next value in case there is a NULL is encountered.
Merge into [dbo].[test] a
using [dbo].[test2] b
on a.col1 = b.col1
when matched then
update
set col2 = COALESCE(a.col2, b.col2),
col3 = COALESCE(a.col3, b.col3),
col4 = COALESCE(a.col4, b.col4);
So in the modified code, if the Table [dbo].[test] col2 is Null then it takes the value from [dbo].[test2]. Since you will be using COALESCE, you will be able to merge any number of values.
Can you show the table Structure.
you can use
UNION ALL Operator follow this link.
https://www.techonthenet.com/sql_server/union.php
OR mixture of INNER JOIN with UPDATE statement combining the condition on WHERE clause

SQL how to check is a value in a col is NOT in another table

Maybe I need another coffee because this seems so simple yet I cannot get my head around it.
Let's say I have a tableA with a col1 where employee IDs are stored.... ALL employee IDs. And the 2nd table, tableB has col2 which lists all employeeID who have a negative evaluation.
I need a query which returns all ID's from col1 from table1 and a newcol which show a '1' for those ID's which do NOT exist in col2 of TableB.
I am doing this in dashDB
One option uses a LEFT JOIN between the two tables:
SELECT a.col1,
CASE WHEN b.col2 IS NULL THEN 1 ELSE 0 END AS new_col
FROM tableA a
LEFT JOIN tableB b
ON a.col1 = b.col2
Alternatively you can achieve your requirement with LEFT JOIN along with IFNULL function as below.
SELECT a.col1,
IFNULL(b.col2, 1) NewCol
FROM tableA a
LEFT JOIN tableB b
ON a.col1 = b.col2

SQL Error: ORA-00905: missing keyword, can someone please tel me whats wrong with the below merge statement

MERGE INTO table_a PARTITION (x) A
USING (SELECT distinct col1,col2 FROM table_b)b
ON (A.col1=B.col1(+) AND A.col2=B.col2(+))
WHEN MATCHED THEN
UPDATE SET col3='Y'
WHEN NOT MATCHED THEN
UPDATE SET col3='N';
The WHEN NOT MATCHED clause cannot perform an UPDATE - what rows is it supposed to update if it didn't find any?
If you want to update A depending on whether there's an existing row in B, one approach is to pull A into the merge query (this assumes your table A has a primary key PK):
MERGE INTO table_a PARTITION (x) tgt
USING (
SELECT A.PK, B.col1, B.col2
FROM table_a A
LEFT JOIN table_b b ON A.col1 = B.col1 AND A.col2 = B.col2) src
ON (src.pk = tgt.pk)
WHEN MATCHED THEN
UPDATE SET col3 = (CASE WHEN src.col1 IS NOT NULL THEN 'Y' ELSE 'N' END);

SQL update where value in other table is

I need to update a value in a table if a specific value exists in another table.
i.e.
update table1
set value1=3
where table2.value2='Y'
There is a key ref1 in both tables- how do i use this key to link these together? Many thanks!
update table1
inner join table2 on table1.ref1 = table2.ref1
set value1 = 3
where table2.value2 = 'Y'
you can write a join between both tables and then make the update from the join.
Something like:
update tableA
set column = b.value
from tableA a
join tableB on a.key = b.key