Find and Replace a String in a Table

The folowing stored procedure will find and replace a string in a table:

CREATE PROCEDURE [dbo].[sp_st_FindANDReplaceString]@Tablename  varchar(20),@FieldName  varchar(20),@FindString  varchar(30),@RepalceString  varchar(30)ASdeclare @sqlstring varchar(8000)Select  @sqlstring  = "Update " +  @Tablename + " Set " +  @FieldName + "= Replace(" + @FieldName + ",'" +  @FindString + "','" + @RepalceString  +"')"execute (@sqlstring)GOEXEC sp_st_FindANDReplaceString ('Orders','ShipName','QUICK','SLOW')

This replaces QUICK to SLOW in the field ShipName of the Orders table.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: