I am trying to update a column of a table A with the values in table B column based on if Table A.col1 = TableB.Col1.
Problem: I overwrite TableA column value with Null if Col1 is not found in TableB.Col1.
My current query is
UPDATE [tableA]
SET col2 = (SELECT col2 FROM [tableB] WHERE [TableB].col1 = [TableA].col1)
How can I avoid this?
Ex: TableA
Col1 Col2
1 100
2 200
3 300
TableB
Col1 Col2
1 1000
3 3000
Resulting table should be:
Table A
Col1 Col2
1 1000
2 200
3 3000
But I get:
Col1 Col 2
1 1000
2 null
3 3000
Any ideas?
You could do:
UPDATE [tableA]
SET col2 = COALESCE(
(SELECT col2 FROM [tableB] WHERE [TableB].col1 = [TableA].col1),
col2)
COALESCE returns the first non-NULL expression among its arguments.
Or, you could do:
UPDATE a
SET col2 = b.col2
FROM TableA a
INNER JOIN
TableB b
ON
a.col1 = b.col1
but you should be aware that this second form is SQL Server dialect, not standard SQL.
You don't want to update the whole table so your query needs a where clause. In this case :
WHERE exists (select 1
from [tableB]
where [TableB].col1=[TableA].col1
and [TableB].col2 is not NULL -- that condition may or may not be needed
)
This should do it, no?
UPDATE [tableA]
SET col2= (select col2 from [tableB] where [TableB].col1=[TableA].col1 and [TableB].col1 IS NOT NULL )
Related
I have two tables, I want to update Col1 in Tbl1 with Col1 in Tbl2. However I want to update for rows where only some characters of Col2.Tbl1 match with Col2.Tbl2
Table 1
PK Col1 Col2
1 NULL abc123xyz
2 NULL 3da234oaz
3 NULL dbc567gyz
4 NULL agc890hyz
5 NULL adc012jyz
Table 2
PK Col1 Col2
1 001 123
2 002 234
3 003 567
4 004 890
5 005 012
Current query:
Update A
set A.Col1 = B.Col1
from Table 1 A, Table 2 B
where A.Col2 like %B.Col1%
But it didn't work.
Try this
Update A
set
A.Col1 = B.Col1
from
Table 1 A, Table 2 B
where
A.Col2 like '%' + B.Col1 + '%'
You are using the wrong column, right? In your current UPDATE you try to find the value of Table2.Col1 in Table1.Col2. In this case there is not match between these columns. You describe the match as follow:
I want to update for rows where only some characters of Tbl1.Col2 match with Tbl2.Col2
So you can use one of the following solutions using CHARINDEX or LIKE:
UPDATE A
SET A.Col1 = B.Col1
FROM Table1 A, Table2 B
WHERE CHARINDEX(B.Col2, A.Col2) > 0
... using the following using a INNER JOIN:
UPDATE A
SET A.Col1 = B.Col1
FROM Table1 A INNER JOIN Table2 B ON CHARINDEX(B.Col2, A.Col2) > 0
... or using the LIKE (but with the correct column):
UPDATE A
SET A.Col1 = B.Col1
FROM Table1 A, Table2 B
WHERE A.Col2 LIKE '%' + B.Col2 + '%'
demo on dbfiddle.uk
i have column and rows in my table as below
col0 col1 col2 col3 col4
----------------------------
1 A 1 100 AA
2 B 2 200 BB
3 B 1 100 AA
4 A 2 200 BB
i want the final result is
col0 col1 col2 col3 col4
----------------------------
1 A 1 100 AA
2 B 2 200 BB
OR
col0 col1 col2 col3 col4
----------------------------
3 B 1 100 AA
4 A 2 200 BB
i want to delete first and second rows OR third and fourth rows but based on col1, as you can see, their's not same with each other rows except with the col0, because the col0 is primary key. how should i do with sql server express 2012?
Here is an option for deleting the first pair of duplicate records. You can assign a row number based on a partition of the four data columns. Do this in a CTE, and then delete all records from that CTE where the row number is 1.
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY col2, col3, col4 ORDER BY col0) rn
FROM yourTable
)
DELETE
FROM cte
WHERE rn = 1
Follow the link below for a demo showing that the logic of my CTE is correct.
Demo
You can use this
DELETE FROM yourTable
WHERE col0 NOT IN ( SELECT MIN(col0) FROM yourTable GROUP BY col1)
I am trying to read records from 'table1' based on created_date. If created_date in table1 is less than current_date, I need to update a Boolean column in table2.
eg:
Table 1
ID Col1 Col2 Created_Date
1 test test 25-Apr-2016
2 test test 23-Apr-2016
Table 2
ID Col11 Col12 Col_Boolean
2 test test false
I need to update column Col_Boolean in table 2 where ID is ID from table 1 for created_date less than current date.
update table_2 set col_boolean 'false'
where id in (select id from table_1 where created_date < sysdate);
update table2 set col_boolean = 'false'
where exists (select null
from table1
where table1.id = table2.id
and table1.created_date < sysdate);
I've a table with four columns and want to update col4(if its col3 value is false) according to col1 of that row which has true value of col3 and its col2 is equal to updated col2.
Data is like
Col1 Col2 Col3 Col4
1 JOhn false NULL
2 Hony false NULL
3 John true NULL
4 Rohn false NULL
5 Hony true NULL
I want that col4 of 1st row would have 3 and col4 of 2nd row would have 5 in it.
My query is
Update tab
set tab.col4 = tab.col1
from table tab
where tab.col3 = true
But it only updates row that has true value.
Just tried it with SQL Fiddle:
create table tbl_SO_19
(
col1 int,
col2 varchar(50),
col3 bit,
col4 int
)
go
insert into tbl_SO_19
values
(1,'John',0,null),
(2,'Hony',0,null),
(3,'John',1,null),
(4,'Rohn',0,null),
(5,'Hony',1,null)
now you can use below query to update it like as you wanted:
Update tbl_SO_19
set col4 = t.col1
from tbl_SO_19 join tbl_SO_19 t on t.col2=tbl_SO_19.col2 and t.col3=1
where tbl_SO_19.col3 = 0
You need to use a self join:
UPDATE a
SET a.col4 = b.col1
FROM mytable b
JOIN (SELECT col1, col2
FROM mytable
WHERE col3 = true) b ON a.col2 = b.col2
WHERE col3 = false
I have a function that returns a table. It takes one parameter and returns one row.
I want to select the values from that function along with columns from another table in the same SELECT staement.
Something like this:
SELECT a.col1, a.col2, b.col1, b.col2
FROM tab1 a INNER JOIN
func1(a.col1) b ON a.col1 = b.col1
WHERE a.col1 IN (123,456,789)
This doesn't work, but is there a way I can do this.
So, for example, if func 1 returns the following for each of the three values in the example:
col1 col2
123 abc
456 def
789 xyz
then I am expecting something like the following results from my query:
col1 col2 col1 col2
123 xxx 123 abc
456 yyy 456 def
789 zzz 789 xyz
I can do it like this, but I'd rather not call the function multiple times for each column I want from the function:
SELECT col1, col2, (SELECT col1 FROM func1(a.col1)), (SELECT col2 FROM func1(a.col1))
FROM tab1 a
WHERE a.col1 IN (123,456,789)
this code is true
alter FUNCTION GETTABLE()
RETURNS #rtnTable TABLE
(
SUM_CD nvarchar(15) NOT NULL,
Name nvarchar(255) NOT NULL
)
AS
BEGIN
DECLARE #TempTable table (SUM_CD Nvarchar(15), name nvarchar(255))
insert into #TempTable(SUM_CD,name) values ('300001','Ahmed')
insert into #TempTable(SUM_CD,name) values ('300002','Mohamed')
insert into #rtnTable
select * from #TempTable
return
END
select * from GLSUMS g inner join dbo.gettable() s on s.SUM_CD=g.SUM_CD
in SQL SERVER 2012