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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update Help

Author  Topic 

tooba
Posting Yak Master

224 Posts

Posted - 2011-06-15 : 21:24:07
Here is my source
Table1
ID ADDR
1 21345 NORWAL BLVD
Table2
ID ADDR
7 21345 NORWAL
My question is I want to update my Table.ID and here is my only link ADDR. I am using this update syntax
UPDATE HG2
SET ID = HG1.ID
FROM HG2
INNER JOIN HG1
ON HG1.ADDR = HG2.SUBSTRING(ADDR,1,(LEN(ADDR) - LEN(LTRIM(RTRIM(REVERSE(SUBSTRING(REVERSE(ADDR),1,charindex(REVERSE(ADDR),' ',1))))))))
Basically I am trying to trim BLVD from TABL1 and try to link to Table2.ADDR and update Table2.ID 7 to Table.ID 1. Please let me where I am doing wrong. Thanks in advance.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-06-16 : 09:37:30
If all you're trying to do is remove 'BLVD' from the string in HG2.ADDR try
HG1.ADDR = REPLACE(HG2.ADDR,'BLVD','')

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-16 : 09:41:29
Don't if it will work but change this
ON HG1.ADDR = HG2.SUBSTRING(ADDR,1,(LEN(ADDR) - LEN(LTRIM(RTRIM(REVERSE(SUBSTRING(REVERSE(ADDR),1,charindex(REVERSE(ADDR),' ',1))))))))

to this
ON HG1.ADDR = SUBSTRING(ADDR,1,(LEN(HG2.ADDR) - LEN(LTRIM(RTRIM(REVERSE(SUBSTRING(REVERSE(HG2.ADDR),1,charindex(REVERSE(HG2.ADDR),' ',1))))))))



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tooba
Posting Yak Master

224 Posts

Posted - 2011-06-16 : 21:04:24
That is just a sample data, i want to remove all Street suffix to just link HOuse# and Street Name, Let me try that "webfred" suggest. Thank you guys.
Go to Top of Page

tooba
Posting Yak Master

224 Posts

Posted - 2011-06-16 : 21:31:19
Awesome its work fine, Thanks for your help.
Go to Top of Page
   

- Advertisement -