The SQL Server 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 TRANSLATE function is:
SELECT TRANSLATE (String_Expression, characters, translation_chars)
The list of arguments available for this TRANSLATE Function is:
- String_Expression: Please specify a valid String Expression. Please refer to the list of SQL String Functions from our SQL Server tutorial.
- 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.
TRANSLATE Function Example
The following String Function query will translate the characters. Please refer to the SQL REPLACE and SQL REVERSE functions.
-- 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
