DB2 10.5 for Linux, UNIX, and Windows

DECIMAL or DEC scalar function

The DECIMAL function returns a decimal representation of a number, a string representation of a number, or a datetime value.

Numeric to Decimal

Read syntax diagramSkip visual syntax diagram
>>-+-DECIMAL-+-------------------------------------------------->
   '-DEC-----'   

>--(--numeric-expression--+----------------------------+--)----><
                          '-,--precision--+----------+-'      
                                          '-,--scale-'        

String to Decimal

Read syntax diagramSkip visual syntax diagram
>>-+-DECIMAL-+-------------------------------------------------->
   '-DEC-----'   

>--(--string-expression--+------------------------------------------------------+--)-><
                         '-,--precision--+------------------------------------+-'      
                                         '-,--scale--+----------------------+-'        
                                                     '-,--decimal-character-'          

Datetime to Decimal

Read syntax diagramSkip visual syntax diagram
>>-+-DECIMAL-+-------------------------------------------------->
   '-DEC-----'   

>--(--datetime-expression--+-------------------------------+---><
                           '-,--precision--+----------+--)-'   
                                           '-,--scale-'        

The schema is SYSIBM.

Numeric to Decimal
numeric-expression
An expression that returns a value of any built-in numeric data type.
precision
An integer constant with a value in the range of 1 to 31.
The default for precision depends on the data type of numeric-expression:
  • 31 for decimal floating-point
  • 15 for floating-point and decimal
  • 19 for big integer
  • 11 for large integer
  • 5 for small integer.
scale
An integer constant in the range of 0 to the precision value. The default is zero.

The result is the same number that would occur if the first argument were assigned to a decimal column or variable with a precision of precision and a scale of scale. Digits are truncated from the end of the decimal number if the number of digits to the right of the decimal separator character is greater than the scale scale. An error is returned if the number of significant decimal digits required to represent the whole part of the number is greater than precision - scale (SQLSTATE 22003).

String to Decimal
string-expression
An expression that returns a value that is a character string or a Unicode graphic-string representation of a number with a length not greater than the maximum length of a character constant. The data type of string-expression must not be CLOB or DBCLOB (SQLSTATE 42884). Leading and trailing blanks are eliminated from the string and the resulting string must conform to the rules for forming an integer, decimal, floating-point, or decimal floating-point constant (SQLSTATE 22018).

The string-expression is converted to the section code page if required to match the code page of the constant decimal-character.

precision
An integer constant with a value in the range 1 to 31 that specifies the precision of the result. If not specified, the default is 15.
scale
An integer constant with a value in the range 0 to precision that specifies the scale of the result. If not specified, the default is 0.
decimal-character
Specifies the single-byte character constant used to delimit the decimal digits in string-expression from the whole part of the number. The character cannot be a digit, plus (+), minus (-), or blank, and it can appear at most once in string-expression (SQLSTATE 42815).

The result is the same number that would result from CAST(string-expression AS DECIMAL(precision, scale)). Digits are truncated from the end of the decimal number if the number of digits to the right of the decimal separator character is greater than the scale scale. An error is returned if the number of significant digits to the left of the decimal character (the whole part of the number) in string-expression is greater than precision - scale (SQLSTATE 22003). The default decimal character is not valid in the substring if a different value for the decimal-character argument is specified (SQLSTATE 22018).

Datetime to Decimal
datetime-expression
An expression that returns a value of type DATE, TIME or TIMESTAMP.
precision
An integer constant with a value in the range 1 to 31 that specifies the precision of the result. If not specified, the default for the precision and scale depends on the data type of datetime-expression as follows:
  • Precision is 8 and scale is 0 for a DATE. The result is a DECIMAL(8,0) value representing the date as yyyymmdd.
  • Precision is 6 and scale is 0 for a TIME. The result is a DECIMAL(6,0) value representing the time as hhmmss.
  • Precision is 14+tp and scale is tp for a TIMESTAMP(tp). The result is a DECIMAL(14+tp,tp) value representing the timestamp as yyyymmddhhmmss.nnnnnnnnnnnn.
scale
An integer constant with a value in the range 0 to precision that specifies the scale of the result. If not specified and a precision is specified, the default is 0.

The result is the same number that would result from CAST(datetime - expression AS DECIMAL(precision, scale)). Digits are truncated from the end of the decimal number if the number of digits to the right of the decimal separator character is greater than the scale scale. An error is returned if the number of significant digits to the left of the decimal character (the whole part of the number) in string-expression is greater than precision - scale (SQLSTATE 22003).

If the first argument can be null, the result can be null; if the first argument is null, the result is the null value.
Note: The CAST specification should be used to increase the portability of applications. For more information, see "CAST specification".

Examples