Groups | Blog | Home
all groups > sql server (alternate) > february 2006 >

sql server (alternate) : how do i replace a substring in sqlserver ?


Jack Vamvas
2/6/2006 4:24:41 PM
here is an example of SUBSTRING/REPLACE

CREATE TABLE #temp(stringRep VARCHAR(60))
INSERT INTO #temp(stringRep) VALUES('hazyCow')
INSERT INTO #temp(stringRep) VALUES('lazyCow')

DECLARE @startPos INT
SET @startPos = 2
UPDATE #temp SET stringRep = REPLACE(stringRep,SUBSTRING(stringRep, 2,
3),'zz')



SELECT * FROM #temp

DROP TABLE #temp

--
Jack Vamvas
__________________________________________________________________
Receive free SQL tips - register at www.ciquery.com/sqlserver.htm
SQL Server Performance Audit - check www.ciquery.com/sqlserver_audit.htm
New article by Jack Vamvas - SQL and Markov Chains -
www.ciquery.com/articles/art_04.asp

[quoted text, click to view]

David Greenberg
2/6/2006 5:50:01 PM
Hi
I have a character field, varchar(60), and I want to replace only a part
of it, a set substring which is always in the same set positions.
Does anyone have an Sql statement or algorithm/sp that could do that
with one call or run ?
Thanks
David
Erland Sommarskog
2/6/2006 10:42:01 PM
David Greenberg (davidgr@iba.org.il) writes:
[quoted text, click to view]

UPDATE tbl
SET col = substring(col, 1, 20) + 'newval' + substring(col, 26, 60)
FROM tbl
WHERE ...


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Hugo Kornelis
2/6/2006 11:46:29 PM
[quoted text, click to view]

Hi David,

Look up the STUFF function in Books Online - this will do exactly what
you want.

--
AddThis Social Bookmark Button