Using TRANSLATE

Contrary to expectations, the TRANSLATE function has nothing to do with translations. It lets you specify multiple string replacements within a single function. It accepts three input arguments:

  • input_string: A string expression that needs to be changed
  • characters: A string expression representing characters that should be replaced
  • translations: A string expression representing characters that will replace characters specified with a second argument

The function returns a string expression of the same type as the first input argument.

In the following example, the TRANSLATE function is used to replace square brackets with parentheses:

SELECT TRANSLATE('[Sales].[SalesOrderHeader]','[]','()');

Here is the output:

(Sales).(SalesOrderHeader) ...

Get SQL Server 2017 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.