SQL REPLACE Function – Replace Text String

REPLACE function is used to replace text string or its parts. It belongs to the category of string functions. I created big overview of T-SQL text functions some time ago.

SQL REPLACE Syntax (3 arguments)

SELECT REPLACE(<Column with text in which we replace part of the text>, <The string we want to replace>, <New string>)
FROM dbo.Table

Function has 3 arguments:

  1. Text field in which the full or partial text replacement is performed
  2. Replaced string
  3. New string

Example of REPLACE usage

Create a variable of type text, where there will be dashes instead of spaces. Use REPLACE to replace the “-” character with a space ” ”

DECLARE @String AS VARCHAR(255) = 'Hi,-I'm-the-text';
SELECT REPLACE(@String,'-',' ') AS String_after_Replace;

sql replace function example

Rate this post
Category: SQL Functions

About Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zednicek, and I have been working as a freelance Data Engineer for roughly 10 years. During this time, I have been publishing case studies and technical guides on this website, targeting professionals, students, and enthusiasts interested in Data Engineering particularly on Microsoft technologies as well as corporate finance and reporting solutions. 🔥 If you found this article helpful, please share it or mention me on your website or Community forum

Leave a Reply

Your email address will not be published. Required fields are marked *