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 2005 Forums
 Transact-SQL (2005)
 Stuff function deletes data & leaves null values?

Author  Topic 

sh0wtym3
Starting Member

9 Posts

Posted - 2010-12-10 : 14:25:30
Hey, I found this nifty stuff function online to help me find and replace the FIRST instance of a subtring:

DECLARE @find varchar(8000)
SELECT @find='FIND_THIS'
UPDATE table
SET col=Stuff(col, CharIndex(@find, col), Len(@find), 'REPLACE_WITH_THIS')

... The problem is, if this statement doesn't find the value (what you put for "FIND_THIS") then for some reason it wipes the column and leaves a NULL value.

How would I prevent this from happening?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-10 : 14:36:51
You could use a WHERE clause to get only rows that have that FIND_THIS.


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

sh0wtym3
Starting Member

9 Posts

Posted - 2010-12-10 : 14:40:22
Sorry I'm still (sort of) new to SQL, what would the syntax for that be?

Maybe something like:

WHERE col LIKE '%@find%'


..?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-10 : 14:46:50
WHERE col LIKE '%'+@find+'%'
should work.


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

- Advertisement -