BigDaddyJay
BigDaddyJay

Reputation: 70

Need to write SQL statement to copy a value from one field to another where a condition is true

I'm not a SQL expert (by any means). I know I can do this programatically in vb.net (which I will if I can't figure out how to do it with an SQL statement).

I need to copy the value of a field in one table to another field in a separate table where a specific condition is true. I've tried my best to model it below:

I want to copy the value

from :Table2.field2 
To   :Table1.field2

where Table1.field1 = Table2.field1 (There are about 3000 rows).

Basically I have some string information in one field for a whole bunch of records where I need to copy that to the same records but in a different field in a different table. Does that make sense?

I know I'm supposed to provide some code to show I'm trying; however, I really don't know where to even start. I'm willing to tough it out on my own if maybe someone can point me in the right direction? I'd appreciate any help/guidance.

Thanks!

Upvotes: 0

Views: 282

Answers (1)

bhamby
bhamby

Reputation: 15499

With SQL Server, you can UPDATE a table using a join:

UPDATE t1
 SET t1.field2 = t2.field2
FROM table1 t1
JOIN table2 t2
  ON t1.field1 = t2.field1

Upvotes: 3

Related Questions