SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to REPLACE substring with another string?

Execute the following Microsoft SQL Server T-SQL scripts in SSMS Query Editor for demonstrating various ways of applying the REPLACE string function.

-- T-SQL REPLACE function usage for string replacement

SELECT REPLACE('United States of America','States of America','Kingdom');

-- United Kingdom

 

-- T-SQL REPLACE function usage for character replacement

SELECT REPLACE('SQL Server 2008 Enterprise Edition','i','*');

-- SQL Server 2008 Enterpr*se Ed*t*on

 

-- Dynamic REPLACE function usage in table SELECT

SELECT TOP (7) ProductID,

ProductName=Name, ChangedName= REPLACE(Name,'bike','bicycle')

FROM AdventureWorks2008.Production.Product

WHERE Name like '%bike%'

ORDER BY NEWID()

/*

ProductID   ProductName             ChangedName

709         Mountain Bike Socks, M  Mountain bicycle Socks, M

876         Hitch Rack - 4-Bike     Hitch Rack - 4-bicycle

877         Bike Wash - Dissolver   bicycle Wash - Dissolver

879         All-Purpose Bike Stand  All-Purpose bicycle Stand

710         Mountain Bike Socks, L  Mountain bicycle Socks, L

*/

 

-- Nested REPLACE example - eliminating special characters from string

DECLARE @Text varchar(max) = 'qw$ert%yuiop!@ASDFGHJKL'

SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(

@Text,

'!',''),'@',''),'#',''),'$',''),'%',''),'^',''),'&',''),'*','')

-- qwertyuiopASDFGHJKL

------------

-- User-defined function (UDF) for cleansing string

------------

CREATE FUNCTION dbo.fnCleanString

               (@InputString NVARCHAR(MAX),

                @KeepNchars   NVARCHAR(512))

RETURNS NVARCHAR(MAX)

  BEGIN

    DECLARE @Pattern NVARCHAR(1024) = '%[^' + @KeepNchars + ']%'

    WHILE PATINDEX(@Pattern,@InputString) >= 1

      SET @InputString = REPLACE(@InputString,Substring(@InputString,

                         PATINDEX(@Pattern,@InputString), 1),'')

    RETURN @InputString

  END

GO

SELECT CleanString=dbo.fnCleanString (N'James Bond 007', N'abcdefghijklmnopqrstuvwxyz')

-- JamesBond

------------

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.