DB2 Version 9.7 for Linux, UNIX, and Windows

RIGHT scalar function

Read syntax diagramSkip visual syntax diagram
>>-RIGHT--(--string-expression--,--length--+--------------------+--)-><
                                           '-,--+-CODEUNITS16-+-'      
                                                +-CODEUNITS32-+        
                                                '-OCTETS------'        

The schema is SYSIBM. The SYSFUN version of the RIGHT function continues to be available.

The RIGHT function returns the rightmost string of string-expression of length length, expressed in the specified string unit. If string-expression is a character string, the result is a character string. If string-expression is a graphic string, the result is a graphic string

string-expression
An expression that specifies the string from which the result is derived. The expression must return a value that is a built-in string, numeric, or datetime data type. If the value is not a string data type, it is implicitly cast to VARCHAR before evaluating the function. A substring of string-expression is zero or more contiguous code points of string-expression.
length
An expression that specifies the length of the result. The expression must return a value that is a built-in numeric, CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type. If the value is not of type INTEGER, it is implicitly cast to INTEGER before evaluating the function. The value must be between 0 and the length of string-expression, expressed in units that are either implicitly or explicitly specified (SQLSTATE 22011) with one exception. If the value is specified as a constant without explicitly specifying the string unit, the value can exceed the length attribute of string-expression in the implicit string unit. If OCTETS is specified and the result is graphic data, the value must be an even number between 0 and twice the length attribute of string-expression (SQLSTATE 428GC).
CODEUNITS16, CODEUNITS32, or OCTETS
Specifies the string unit of length.

CODEUNITS16 specifies that length is expressed in 16-bit UTF-16 code units. CODEUNITS32 specifies that length is expressed in 32-bit UTF-32 code units. OCTETS specifies that length is expressed in bytes.

If the string unit is specified as CODEUNITS16 or CODEUNITS32, and string-expression is a binary string or bit data, an error is returned (SQLSTATE 428GC). If the string unit is specified as OCTETS and string-expression is a graphic string, length must be an even number; otherwise, an error is returned (SQLSTATE 428GC). If a string unit is not explicitly specified, the data type of the result determines the unit that is used. If the result is graphic data, length is expressed in two-byte units; otherwise, it is expressed in bytes. For more information about CODEUNITS16, CODEUNITS32, and OCTETS, see "String units in built-in functions" in "Character strings".

The string-expression is padded on the right with the necessary number of padding characters so that the specified substring of string-expression always exists. The character used for padding is the same character that is used to pad the string in contexts where padding would occur. For more information on padding, see "String assignments" in "Assignments and comparisons".

The result of the function is a varying-length string with a length attribute that depends on how length and the string unit are specified. If length is not specified using a constant or the string unit is explicitly specified, then the length attribute is the same as the length attribute of string-expression. If length is specified using a constant and the string unit is not specified, then the length attribute is the maximum of length and the length attribute of string-expression. The data type of the result depends on the data type of string-expression:
  • VARCHAR if string-expression is CHAR or VARCHAR
  • CLOB if string-expression is CLOB
  • VARGRAPHIC if string-expression is GRAPHIC or VARGRAPHIC
  • DBCLOB if string-expression is DBCLOB
  • BLOB if string-expression is BLOB
The actual length of the result (in string units) is length.

If any argument can be null, the result can be null; if any argument is null, the result is the null value.

Examples: