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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple replace question 2

Status
Not open for further replies.

Naoise

Programmer
Dec 23, 2004
318
IE
declare @foo varchar(50)
set @foo = '1.1.1.1'

select replace(@foo, '1.1','1.2')

This gives me 1.2.1.2 as it replaces all occurrences of 1.1 in @foo, how would I only replace the beginning of @foo with 1.2?

I will always know the length of the string to replace if that helps?
 
In that particular case:
Code:
declare @foo varchar(50)
set @foo = '1.1.1.1'

select replace(@foo, '1.1.','1.2.')
But if you want to replce just the first occurance of the string no matter where it resides, you could try:
Code:
[COLOR=blue]declare[/color] @foo [COLOR=blue]varchar[/color](50)
[COLOR=blue]declare[/color] @stringtobereplaced [COLOR=blue]varchar[/color](50)
[COLOR=blue]declare[/color] @stringforreplace   [COLOR=blue]varchar[/color](50)

[COLOR=blue]declare[/color] @first [COLOR=blue]int[/color]
[COLOR=blue]set[/color] @foo = [COLOR=red]'1.1.1.1'[/color]
[COLOR=blue]set[/color] @stringtobereplaced = [COLOR=red]'1.1'[/color]
[COLOR=blue]set[/color] @stringforreplace   = [COLOR=red]'1.2'[/color]
[COLOR=blue]SET[/color] @first = [COLOR=#FF00FF]CHARINDEX[/color](@stringtobereplaced,@foo)
[COLOR=blue]IF[/color] @first <> 0
   [COLOR=blue]BEGIN[/color]
       [COLOR=blue]SET[/color] @foo = [COLOR=#FF00FF]LEFT[/color](@foo, @first-1)+
                  @stringforreplace   +
                  [COLOR=#FF00FF]SUBSTRING[/color](@foo,@first+LEN(@stringtobereplaced),8000)
   [COLOR=blue]END[/color]
[COLOR=blue]SELECT[/color] @foo

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
If you always know the length and the string you want to replace is always at the beginning of the string, you can do this:

Code:
declare @foo varchar(50)
set @foo = '1.1.1.1'

select replace(left(@foo, 3), '1.1','1.2') + right(@foo, len(@foo)- 3)

Where 3 is the length, which you can put into a varable if you want.


[monkey][snake] <.
 
I don't think that is going to work

declare @foo varchar(50)
set @foo = '1.1.1.1.1'

select replace(@foo, '1.1','1.2')

In effect I wish to replace the first Len('1.1') characters with '1.2'

The '1.1' argument could be anything, '1.3.5.1.2.7' etc
 
declare @foo varchar(50)
set @foo = '1.1.1.1'

select replace((left(@foo,3)), '1.1','1.2')+right(@foo,len(@foo)-3)
 
This will replace the first occurrence:

Code:
[COLOR=blue]declare[/color] @foo [COLOR=blue]varchar[/color](50)
[COLOR=blue]set[/color] @foo = [COLOR=red]'1.1.1.1'[/color]

[COLOR=green]--here goes
[/color][COLOR=blue]select[/color] [COLOR=#FF00FF]stuff[/color](@foo, [COLOR=#FF00FF]charindex[/color]([COLOR=red]'1.1'[/color], @foo), len([COLOR=red]'1.1'[/color]), [COLOR=red]'1.2'[/color])

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
I see a lot of options here. I guess the question is, what do you want to see when you get this for @foo:

1.7.9.1.1.3.2.1.1 ?

Ignorance of certain subjects is a great part of wisdom
 
The main question is, do you always want to replace the beginning of the string?? If not, then none of these solutions will do.

[monkey][snake] <.
 
Hey Alex, that's some pretty cool [!]stuff[/!] code.

-George

"the screen with the little boxes in the window." - Moron
 
I don't get to use stuff very often. Fun times :)

Naoise - glad you've got it working!



Ignorance of certain subjects is a great part of wisdom
 
The only reason Alex beat me to that code was because I had to leave for work. :p

[COLOR=#aa88aa black]Cum catapultae proscriptae erunt tum soli proscript catapultas habebunt.[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top