Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Compare colums

Author  Topic 

Jsolomon
Starting Member

13 Posts

Posted - 2010-11-21 : 16:55:11
What I need to do is compare the value of column A in Table 1 to the value of Column A in Table 2 if equal the column A in Table 1 is updated to the value of column B in Table 2. Basicly column A will have 170 different values that need to change to a new value.

Jeff

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2010-11-21 : 17:02:41
If table1 Column A = Table2 Column A, then set Table 1 column A = Table 2 column B?

Update Table1
Set ColumnA = Table2.ColumnB
FROM Table1 inner join Table2
on Table1.KeyColumn = Table2.KeyCOlumn
Where Table1.ColumnA = Table2.ColumnA

Not really quite enough to give a completely accurate response...





Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

Jsolomon
Starting Member

13 Posts

Posted - 2010-11-21 : 17:13:35
I think you have it. What I have is a a list of about 170 different auto types in a table with 800,000 records. EXAMPLE table 1 column A might contain "F TRUCKS", my new table 2 column A will contain "F TRUCKS" column B will be "F150".

so when I find F TRUCKS in table 1 column A change it to F150.

better example?

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-21 : 17:18:40
Think dataguru1971' update covers that. Just replace the join with the where clause
Update Table1
Set ColumnA = Table2.ColumnB
FROM Table1 inner join Table2
on Table1.ColumnA = Table2.ColumnA



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2010-11-21 : 17:21:21
What about F250? Is the presumption that all 'F Trucks' will be F150's?

Might be better to test out the results in a select statement, placing the new result alongside the original column to make sure the results are as expected.

If you are confident in the ColumnA of both tables, you can use that in the join instead of the where clause, but there is really no difference.

Might want to check results using a select statement first to make sure the right amount of records are affected, and apply a WHERE condition to limit to only F Trucks.

Select Table1.ColumnA, Table2.ColumnB
FROM Table1 inner join Table2
on Table1.Key = Table2.Key
AND Table1.ColumnA = Table2.COlumnA
Where Table1.ColumnA = 'F Trucks'




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

Jsolomon
Starting Member

13 Posts

Posted - 2010-11-21 : 17:34:52
You were right. F Trucks can equal f150 or f250 or f350. I can use a "and" for two cloumns from table 1.

Thanks
Go to Top of Page
   

- Advertisement -