CHAR

Click to skip syntax diagram

Datetime to Character

Read syntax diagramSkip visual syntax diagram>>-CHAR--(--datetime-expression--+--------------+--)-----------><
                                 '-,--+-ISO---+-'
                                      +-USA---+
                                      +-EUR---+
                                      +-JIS---+
                                      '-LOCAL-'
 

Graphic to Character

Read syntax diagramSkip visual syntax diagram>>-CHAR--(--graphic-expression--+------------+--)--------------><
                                '-,--integer-'
 

Character to Character

Read syntax diagramSkip visual syntax diagram>>-CHAR--(--character-expression--+------------+--)------------><
                                  '-,--integer-'
 

Integer to Character

Read syntax diagramSkip visual syntax diagram>>-CHAR--(--integer-expression--)------------------------------><
 

Decimal to Character

Read syntax diagramSkip visual syntax diagram>>-CHAR--(--decimal-expression--+----------------------+--)----><
                                '-,--decimal-character-'
 

Floating-point to Character

Read syntax diagramSkip visual syntax diagram>>-CHAR--------------------------------------------------------->
 
>--(--floating-point-expression--+----------------------+--)---><
                                 '-,--decimal-character-'
 

The CHAR function returns a fixed-length character-string representation of:

The first argument must be a built-in data type other than a BINARY, VARBINARY, or BLOB.

The result of the function is a fixed-length character string. If the first argument can be null, the result can be null; if the first argument is null, the result is the null value.

Datetime to Character

datetime-expression
An expression that is one of the following three built-in data types
date
The result is the character-string representation of the date in the format specified by the second argument. If the second argument is not specified, the format used is the default date format. If the format is ISO, USA, EUR, or JIS, the length of the result is 10. Otherwise the length of the result is the length of the default date format. For more information see String representations of datetime values.
time
The result is the character-string representation of the time in the format specified by the second argument. If the second argument is not specified, the format used is the default time format. The length of the result is 8. For more information see String representations of datetime values.
timestamp
The second argument is not applicable and must not be specified.

The result is the character-string representation of the timestamp. The length of the result is 26.

The CCSID of the string is the default SBCS CCSID at the current server.
ISO, EUR, USA, or JIS
Specifies the date or time format of the resulting character string. For more information, see String representations of datetime values.
LOCAL
Specifies that the date or time format of the resulting character string should come from the DATFMT, DATSEP, TIMFMT, and TIMSEP attributes of the job at the current server.

Graphic to Character

graphic-expression
An expression that returns a value that is a built-in graphic-string data type.
integer
An integer constant that specifies the length attribute for the resulting fixed length character string. The value must be between 1 and 32766 (32765 if nullable).

If the second argument is not specified:

The actual length is the same as the length attribute of the result. If the length of the graphic-expression is less than the length of the result, the result is padded with blanks up to the length of the result. If the length of the graphic-expression is greater than the length attribute of the result, truncation is performed. A warning (SQLSTATE 01004) is returned unless the truncated characters were all blanks.

The CCSID of the string is the default CCSID of the current server.

Character to Character

character-expression
An expression that returns a value that is a built-in character-string data type.
integer
An integer constant that specifies the length attribute for the resulting fixed length character string. The value must be between 1 and 32766 (32765 if nullable). If the first argument is mixed data, the second argument cannot be less than 4.

If the second argument is not specified:

The actual length is the same as the length attribute of the result. If the length of the character-expression is less than the length of the result, the result is padded with blanks up to the length of the result. If the length of the character-expression is greater than the length attribute of the result, truncation is performed. A warning (SQLSTATE 01004) is returned unless the truncated characters were all blanks.

The CCSID of the string is the CCSID of the character-expression.

Integer to Character

integer-expression
An expression that returns a value that is a built-in integer data type (either SMALLINT, INTEGER, or BIGINT).

The result is the fixed-length character-string representation of the argument in the form of an SQL integer constant. The result consists of n characters that are the significant digits that represent the value of the argument with a preceding minus sign if the argument is negative. The result is left justified.

The CCSID of the string is the default SBCS CCSID at the current server.

Decimal to Character

decimal-expression
An expression that returns a value that is a built-in decimal data type (either DECIMAL or NUMERIC). If a different precision and scale is desired, the DECIMAL scalar function can be used to make the change.
decimal-character
Specifies the single-byte character constant that is used to delimit the decimal digits in the result character string. The character must be a period or comma. If the second argument is not specified, the decimal point is the default decimal point. For more information, see Decimal point.

The result is a fixed-length character string representation of the argument. The result includes a decimal character and up to p digits, where p is the precision of the decimal-expression with a preceding minus sign if the argument is negative. Leading zeros are not returned. Trailing zeros are returned.

The length of the result is 2+p where p is the precision of the decimal-expression. This means that a positive value will always include one trailing blank.

The CCSID of the string is the default SBCS CCSID at the current server.

Floating-point to Character

floating-point expression
An expression that returns a value that is a built-in floating-point data type (DOUBLE or REAL).
decimal-character
Specifies the single-byte character constant that is used to delimit the decimal digits in the result character string. The character must be a period or comma. If the second argument is not specified, the decimal point is the default decimal point. For more information, see Decimal point.

The result is a fixed-length character-string representation of the argument in the form of a floating-point constant. The length of the result is 24. If the argument is negative, the first character of the result is a minus sign. Otherwise, the first character is a digit. If the argument is zero, the result is 0E0. Otherwise, the result includes the smallest number of characters that can be used to represent the value of the argument such that the mantissa consists of a single digit other than zero followed by a period and a sequence of digits.

If the number of characters in the result is less than 24, then the result is padded on the right with blanks.

The CCSID of the string is the default SBCS CCSID at the current server.

Note

Syntax alternatives: When the first argument is numeric, or the first argument is a string and the length argument is specified, the CAST specification should be used for maximal portability. For more information, see CAST specification.

Examples