How to Use the REPLACE() Function in SQL Server: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-21 09:39:01   5508  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

REPLACE function replaces all occurrences of a specified string value with another string value.

Syntax:


REPLACE(String_Expression, Pattern , Replacement_Value)

Example: All .COM strings are replaced with .NET

Code:



Select Email, REPLACE(Email, '.com', '.net') as Email
from  TableEmployee


Replace function in SQL Server

Prerequisite Code


CREATE TABLE TableEmployee(
	FirstName varchar(50),
	LastName varchar(50),
	Email varchar(50)
)

INSERT INTO TableEmployee VALUES('Rambo', 'Azmi', 'Rambo@aaa.com')
INSERT INTO TableEmployee VALUES('Azam', 'Ali', 'Azam@aaa.com')
INSERT INTO TableEmployee VALUES('Inza', 'Hoque', 'Rambo@aaa.com')
INSERT INTO TableEmployee VALUES('Jaman', 'Sk', 'Jaman@aaa.com')
INSERT INTO TableEmployee VALUES('Samser', 'Alam', 'Samser@aaa.com')

SELECT * FROM TableEmployee