The SQL TRANSLATE() is a string function that is used to replaces a sequence of characters in a string with another sequence of characters.

The function replaces a single character at a time. Until SQL Server 2016, you must have used  REPLACE() function to replace each character.


TRANSLATE (inputstring, from_text, to_text)

Inputstring  is any input string.

from_text is a characters that should be replaced from input string.

to_text is a new characters.

from_text and to_text should have same length.  If the lengths of from_text and to_text are different, the TRANSLATE function will return an error.

Lets look at an example of TRANSLATE function in SQL.

Using TRANSLATE Function to replace characters in string

Following statement uses Translate function and replace the * with and ) with ! in string ‘Hello*world)’

DECLARE @string AS VARCHAR(50)= 'Hello*World)'

SELECT @string AS String, TRANSLATE(@string,'*)','-!') AS ReplacedString


Here we have a string ‘1{2}3@5’, Lets replace curly braces { and } with and +, and @ with * within string using Replace and Translate function.

DECLARE @string AS VARCHAR(50)= '1{2}3@5'

SELECT @string AS String,
REPLACE(REPLACE(REPLACE(@string,'{', '-'), '}', '+'), '@', '*') AS UsingReplaceFn,
TRANSLATE(@string,'{}@','-+*') AS UsingTranslateFn

As you can see, Replace and Translate functions replace the characters within string in desired format.

Also Read..









SQL Basics TutorialSQL Advance TutorialSSRSInterview Q & A
SQL Create tableSQL Server Stored ProcedureCreate a New SSRS Project List Of SQL Server basics to Advance Level Interview Q & A
SQL ALTER TABLESQL Server MergeCreate a Shared Data Source in SSRSSQL Server Question & Answer Quiz
SQL DropSQL Server PivotCreate a SSRS Tabular Report / Detail Report
..... More.... More....More
Power BI TutorialAzure TutorialPython TutorialSQL Server Tips & Tricks
Download and Install Power BI DesktopCreate an Azure storage accountLearn Python & ML Step by stepEnable Dark theme in SQL Server Management studio
Connect Power BI to SQL ServerUpload files to Azure storage containerSQL Server Template Explorer
Create Report ToolTip Pages in Power BICreate Azure SQL Database ServerDisplaying line numbers in Query Editor Window

 129 total views,  1 views today

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.