Update values from one column in same table to another in SQL Server
This works for me
select * from stuff
update stuff
set TYPE1 = TYPE2
where TYPE1 is null;
update stuff
set TYPE1 = TYPE2
where TYPE1 ='Blank';
select * from stuff
SQL Server : update column with value from same table based on choice of values
You can use a correlated update.
UPDATE t2
SET qty = (select SUM(qty) from Inventory where item_id=t2.item_id and ENTRYTYPE = 20)
FROM Inventory t2
where ENTRYTYPE=10 AND qty=0
To restrict the item_id's that get updated, use a where
condition.
SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?
update QuestionTrackings q
inner join QuestionAnswers a
on q.AnswerID = a.AnswerID
set q.QuestionID = a.QuestionID
where q.QuestionID is null -- and other conditions you might want
I recommend to check what the result set to update is before running the update (same query, just with a select):
select *
from QuestionTrackings q
inner join QuestionAnswers a
on q.AnswerID = a.AnswerID
where q.QuestionID is null -- and other conditions you might want
Particularly whether each answer id has definitely only 1 associated question id.
How can I copy data from one column to another in the same table?
How about this
UPDATE table SET columnB = columnA;
This will update every row.
Update in the same column from the same table
We could try using COALESCE
to conditionally replace a NULL
year of birth with a non NULL
value:
SELECT
ID,
EMPLOYEE_ID,
COALESCE(YEAR_OF_BIRTH, MAX(YEAR_OF_BIRTH) OVER (PARTITION BY EMPLOYEE_ID)) AS YEAR_OF_BIRTH,
PROJECT_ID
FROM yourTable;
How to update a column with data from another column in SQL Server?
You can try this as shown below.
UPDATE a
SET a.UserName = b.Kwd
FROM test_table1 a INNER JOIN test_table1 b ON a.Id= b.Id
You can also try the following query.
update test_table1
set test_table1.UserName = B.Kwd
from test_table1 B
You can follow the link Inner join update in SQL Server
Here is an example with sample data.
create table test_table1 (PelID int, Kwd varchar(10), UserName varchar(10))
insert into test_table1 Values (1, 'A', 'B'), (2, 'K', 'P'), (3, 'N', 'S'), (4, 'G', 'H'), (5, 'T', 'F')
Select * from test_table1
UPDATE a
SET a.UserName = b.Kwd
FROM test_table1 a INNER JOIN test_table1 b ON a.PelID = b.PelID
Select * from test_table1
update test_table1
set test_table1.UserName = B.Kwd
from test_table1 B
Select * from test_table1
This output can be checked on the link
SQL UPDATE on with data from same table
Asuming [SN]
is being augmented with -RG
Example
with cte as (
Select *
,NV = max(FID) over (partition by replace(SN,'-RG','') )
From YourTable
)
Update cte set FID = NV
--Where FID is null -- Optional
The Updated Table
ID SN FID
1 12345 1
2 1122 2
3 12345-RG 1
4 1122-RG 2
Update every row with a value from another column from same table
How about this:
UPDATE dbo.YourTable
SET Column3 = SUBSTRING(Column2, LEN(Column2)-3, LEN(Column2))
If needed, you can also include a WHERE
clause to limit the rows being updated, e.g.:
UPDATE dbo.YourTable
SET Column3 = SUBSTRING(Column2, LEN(Column2)-3, LEN(Column2))
WHERE Column3 IS NULL
or something like that.
Bulk update of one column with other column in same table
No need for a FROM clause:
UPDATE tbl
SET col_1 = to_tsvector(col_2);
Related Topics
Postgresql Prefix Wildcard for Full Text
What Is the Equivalent of 'Go' in MySQL
How to Find the Average Value in a Column of Dates in SQL Server
SQL Server 2008: Ordering by Datetime Is Too Slow
What Are the Pros and Cons of Using Multi Column Primary Keys
Select Multiple (Non-Aggregate Function) Columns with Group By
SQL Merge Tables Side-By-Side with Nothing in Common
How to Convert a Bunch of Boolean Columns to a Single Bitmap in Postgresql
Rename a Constraint in SQL Server
Referencing Outer Query's Tables in a Subquery
Drop Default Constraint on a Column in Tsql
Update Values from One Column in Same Table to Another in SQL Server
Is MySQL Limit Applied Before or After Order By
Best Way to Compare Dates Without Time in SQL Server