Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

How to Replace String in SQL Server

by suresh

In this article we will show you, How to Replace String in SQL Server table column with example. This is one of the SQL Server Interview Question that you are going to face in interviews. For this example, We are going to use the below shown data

How to Replace String in SQL Server 1

Replace String in SQL Server Example 1

In this replace string example we will declare a string variable, and then we are going to replace a part of a string with new string using the SQL Server Replace Function.

-- SQL Server Query to Replace String
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'

OUTPUT
How to Replace String in SQL Server 2

Replace String in SQL Server Example 2

This replace string example will show you, How to replace string in SQL Server SELECT Statement. Here we are going to use the REPLACE Function while we are selecting data from the table.

-- SQL Replace Part of a String
USE [SQL Tutorial]
GO
SELECT [EmpID]
      ,[Full Name]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[Email Adress]
      ,REPLACE([Email Adress], 'microsoft.com', 'tutorialgateway.org') AS [New EmailID]
  FROM [StringExample]

OUTPUT
How to Replace String in SQL Server 3

Replace String in SQL Example 3

In this example we will show you, How to replace string in SQL UPDATE Statement. Here we are going to use the REPLACE Function in Update Statement.

-- SQL Replace Part of a String
USE [SQL Tutorial]
GO
UPDATE [StringExample]
SET [Email Adress] = REPLACE([Email Adress], 'xyz.com', 'tutorialgateway.org')

Now let me show you the Updated table

OUTPUT
How to Replace String in SQL Server 4

Thank You for Visiting Our Blog

Placed Under: SQL, SQL Interview Questions

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy