Skip to content

Commit 94de648

Browse files
authored
TRANSLATE vs REPLACE
I realised, after making a now accepted change, that actually `TRANSLATE` and multiple `REPLACE` statements are not equivalent. If a character is replaced to one that is also appears later in the translatations character string, the character will not be replaced. For `REPLACE`, however, this is not true. `REPLACE` will replace all instances of that character, including those that were replaced in one of the nested functions. I have therefore added this information in the Remarks section.
1 parent 04dd062 commit 94de648

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

docs/t-sql/functions/translate-transact-sql.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ Returns a character expression of the same data type as `inputString` where char
4646

4747
`TRANSLATE` will return an error if *characters* and *translations* expressions have different lengths. `TRANSLATE` will return NULL if any of the arguements are NULL.
4848

49-
The behavior of the `TRANSLATE` function is equivalent to using multiple [REPLACE](../../t-sql/functions/replace-transact-sql.md) functions.
49+
The behavior of the `TRANSLATE` function is similar to using multiple [REPLACE](../../t-sql/functions/replace-transact-sql.md) functions. `TRANSLATE` does not, however, replace a character more than once. This is disimilar to multiple `REPLACE` functions, as each use would replace all relevant characters. For example:
50+
51+
```sql
52+
SELECT TRANSLATE('abcdef','abc','bcd') AS Translated,
53+
REPLACE(REPLACE(REPLACE('abcdef','a','b'),'b','c'),'c','d') AS Replaced;
54+
```
55+
The results are:
56+
Translated |Replaced |
57+
---------|--------- |
58+
bcddef |ddddef |
5059

5160
`TRANSLATE` is always SC collation aware.
5261

0 commit comments

Comments
 (0)