Replace String in SQL Server

How to Replace String in SQL Server table column with example? For this Interview Question, We are going to use the below-shown data.

Source Table 1

Replace String in SQL Server Example

In this example, we will declare a string variable and then replace a part of a string with new text using the REPLACE Function. Refer to the SQL REPLACE Function article on the SQL Server tutorial page.

DECLARE @String_Expression varchar(50)
SET @String_Expression = 'Blog Provides Free Tutorial on SQL Server'

SELECT REPLACE (@String_Expression, 
                'Blog','Tutorial Gateway'
                ) AS 'SQL Server Replace'
Replace String 2

 This example shows how to replace a string in the SELECT Statement. Here, we are going to use the REPLACE Function while we are selecting data from the SQL Server table. Refer to the SELECT Statement in SQL.

SELECT [EmpID]
      ,[Full Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[Email Adress]
      ,REPLACE([Email Adress], 'microsoft.com', 'tutorialgateway.org') AS [New EmailID]
  FROM [StringExample]
How to Replace String 3

Replace string in Update Statement

In this example, we will show you how to replace strings in an UPDATE Statement. Here we are going to use the REPLACE Function in an UPDATE Statement. Also, check the SQL UPDATE Statement and SQL reverse string articles.

UPDATE [StringExample]
SET [Email Adress] = REPLACE([Email Adress], 'xyz.com', 'tutorialgateway.org')
Output 4
Categories SQL