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
 Replace value

Author  Topic 

Lynx
Starting Member

4 Posts

Posted - 2010-05-30 : 04:48:17
Hi,

I want to replaced a value inside a string. The table look like this:
Id Value
1 34,52,68
2 48,34,66

How do I replace the value 34 with 99 to make the table look like this:
Id Value
1 99,52,68
2 48,99,66

Is it possible, and how do I do it?

Thanks in advance!
Carl

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-30 : 05:05:17
[code]
update t
set Value = substring (NewValue, 2, len(NewValue) - 2)
from (
select Value,
NewValue = replace(',' + Value + ',', ',34,', ',99,')
from yourtable
) t
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Lynx
Starting Member

4 Posts

Posted - 2010-05-30 : 06:21:21
Hi,

Thank you!

You saved my day!

Carl
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-05-30 : 06:22:15
Update yourtable set value=REPLACE(value,'34','99')

PBUH
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-30 : 09:41:33
quote:
Originally posted by Idera

Update yourtable set value=REPLACE(value,'34','99')

PBUH



that will update 34,134 to 99,199


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-05-30 : 10:12:32
Possibly add:

WHERE ',' + Value + ',' LIKE '%,34,%'

to reduce the number of rows updated (and LOG space used) to just those that qualify
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-30 : 11:13:34
quote:
Originally posted by Kristen

Possibly add:

WHERE ',' + Value + ',' LIKE '%,34,%'

to reduce the number of rows updated (and LOG space used) to just those that qualify



Good point. I missed that


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-31 : 03:53:23
This is one reason why you need to apply normalisation

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-05-31 : 04:10:11
Good point. I missed that

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-31 : 05:09:56
quote:
Originally posted by Kristen

Good point. I missed that




Duplicate replies are not allowed

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-05-31 : 05:19:33
Mine has a smiley ... do you want to borrow my Levenshtein algorithm?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-31 : 05:22:48
quote:
Originally posted by Kristen

Mine has a smiley ... do you want to borrow my Levenshtein algorithm?


CLEVER

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-05-31 : 07:03:41
hmmmm nice points here a lot

KaShYaP
Go to Top of Page
   

- Advertisement -