Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String data manipulation and table update

Status
Not open for further replies.

DougNaf

Programmer
Jun 2, 2005
32
US
I need to make changes to an address field. As an example an address needs to be changed from "123 CO RD J" to "123 COUNTY ROAD J" or "1234 SH 108" to "1234 STATE HIGHWAY 108"

IE change "CO RD" to "COUNTY ROAD" for all occurances in an address string. "CO RD" will NOT always be in the same position of the field.

I need to read the SQL record, manipulate the field and then write the new recalculated string back to the field.

Thanks in advance for suggestions.

Doug

 
That is exactly what the REPLACE function was designed for.

[tt][blue]
Select Replace(YourColumnName, 'CO RD', 'COUNTY ROAD')
From YourTable

[/blue][/tt]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Try something like
Code:
declare @OriginalStr char(7), @NewStr char(13)
set @OriginalStr = ' CO RD '
set @NewStr = ' COUNTY ROAD '

select replace (' ' + Ltrim(rtrim(AddressField)) + ' ', @OriginalStr, @NewStr))
Assuming we're dealing with the character field.
 
I used the first example:
Select Replace(YourColumnName, 'CO RD', 'COUNTY ROAD')
From YourTable

Then ran the statement:

Select Replace(YourColumnName, 'ST RT', 'STATE ROUTE')
From YourTable

One of the original fields go back to CO RD that had been changed to COUNTY ROAD.

Is there some statement I need to include that actually writes the changes to the db?

Thanks



 
The simplest form is:

Code:
Update YourTableName
Set    YourColumnName = Replace(YourColumnName, 'ST RT','STATE ROUTE')

It may be faster to include a where clause so that only rows containing the search string are actually updated, like this:

Code:
Update YourTableName
Set    YourColumnName = Replace(YourColumnName, 'ST RT','STATE ROUTE')
Where  YourColumnName Like '%ST RT%'



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
You need to use update command instead. But I would suggest my code instead of gmmastros in your case, so 'BOCO RD' would not become 'COUNTY ROAD', for example.

In my code change select replace to

update Address set AddressField = rtrim(ltrim(replace(' ' + AddressField+ ' ',@OldStr, @NewStr)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top