SQL TRANSLATE Function

The SQL TRANSLATE String Function is used to translate the string expression by replacing the specified characters with a new set of characters. The syntax of the SQL Server TRANSLATE function is:

SELECT TRANSLATE (String_Expression, characters, translation_chars)

The list of arguments available for this string TRANSLATE is:

  • String_Expression: Please specify a valid String Expression
  • characters: Here, you have to specify the characters that you want to replace in string_expression
  • translation_chars: This SQL Server function will replace the characters with these translation_chars. Remember, the type and length of the second and third arguments should be the same.

SQL TRANSLATE Example

The following String Function query will translate the characters using the Translate function.

-- SQL Server Translate Example

-- Finds and replace (){} with [][]
SELECT TRANSLATE('4 * (6+5) / {9+6}', '(){}', '[][]') AS NewExp

-- Finds and replace {} with []
SELECT TRANSLATE('{a + b} * {a - b}', '{}', '[]') AS NewExp

-- Replaces * with +
SELECT TRANSLATE('{a + b} * (a - b)', '*', '+') AS NewExp

-- Finds and replace *() with +[]
SELECT TRANSLATE('{a + b} * (a - b)', '*()', '+[]') AS NewExp
SQL TRANSLATE Function 1