TRANSLATE scalar function syntax

The syntax of the TRANSLATE scalar function depends on whether NPS compatibility mode is being used.

Whether NPS compatibility mode is being used depends on the setting of the SQL_COMPAT global variable:
  • When SQL_COMPAT='NPS', the syntax of the TRANSLATE scalar function is as described in Syntax of the TRANSLATE scalar function when SQL_COMPAT='NPS'. For example:
    translate('12345', '143', 'ax')
    returns:
    a2x5
    In the string '12345':
    • The character 1 is translated to a.
    • The character 4 is translated to x.
    • The character 3 does not have a corresponding character in the "to" string, so it is removed.
  • Otherwise, the syntax of the TRANSLATE scalar function is as described in TRANSLATE scalar function. For example:
    translate('12345', 'ax', '143')
    returns:
    a2 x5
    In the string '12345':
    • The character 1 is translated to a.
    • The character 4 is translated to x.
    • The character 3 does not have a corresponding character in the "to" string, so it is replaced with a padding character. The default padding character is a blank.

Syntax of the TRANSLATE scalar function when SQL_COMPAT='NPS'

If SQL_COMPAT='NPS', the syntax of the TRANSLATE scalar function is:
Read syntax diagramSkip visual syntax diagram TRANSLATE ( char-string-exp , from-string-exp , to-string-exp )

This function converts all the characters in char-string-exp that also occur in from-string-exp to the corresponding characters in to-string-exp. If from-string-exp is longer than to-string-exp, occurrences of the extra characters in from-string-exp are removed from char-string-exp.

char-string-exp
The string that is to be converted. The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function.
from-string-exp
A string of characters that, if found in char-string-exp, are to be converted to the corresponding character in to-string-exp.

The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function. If from-string-exp contains duplicate characters, the first one found will be used, and the duplicates will be ignored. If to-string-exp is longer than from-string-exp, the surplus characters will be ignored. If to-string-exp is specified, from-string-exp must also be specified.

to-string-exp
A string of characters to which certain characters in char-string-exp arre to be converted.

The expression must return a value that is a built-in CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, numeric, or datetime data type. If the value is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it is implicitly cast to VARCHAR before evaluating the function. If a value for to-string-exp is not specified, and the data type is not graphic, all characters in char-string-exp will be in monocase; that is, the characters a-z will be converted to the characters A-Z, and other characters will be converted to their uppercase equivalents, if they exist. For example, in code page 850, é maps to É, but ÿ is not mapped, because code page 850 does not include Ÿ. If the code point length of the result character is not the same as the code point length of the source character, the source character is not converted.