DB2 DECIMAL data type


Describe DB2 z/OS DECIMAL data type

Usage Format

DECIMAL(PRECISION, SCALE)

  • PRECISION refers to the maximum number of digits that are present in the number, and
  • SCALE refers to the maximum number of decimal places.
For example: 123456.789 has a PRECISION of 9, and a SCALE of 3.
So the maximum allowed value for decimal(4,2) is 99.99

Default PRECISION is 5, and SCALE is 0, so
"COLNAME DECIMAL == COLNAME DECIMAL(5, 0)"

Notice:
1. The sign(+/-) does not occupy a PRECISION length, because packed decimal number format rule; refer to "Storage Format" section for details.
2. PRECISION must be greater than (or equal to) SCALE; otherwise, DDL definition will failure, for example,
    CREATE TABLE TESTTAB(A DECIMAL(2, 4))
    And if PRECISION is equal to SCALE, the acceptable value cannot greater than 1.

Value Truncate

DECIMAL value could be truncated if only SCALE part is longer, for example
CREATE TABLE TESTTAB(A DECIMAL(4, 2))
1.  INSERT INTO TESTTAB VALUES(12.345); # 12.34 is inserted, the last 5 is truncated.
Otherwise, failure reported.
2.  INSERT INTO TESTTAB VALUES(123.45); # SQL0413N  Overflow occurred during numeric data type conversion
Because 123 is exceed the precision, and it will not be truncated silently.

Storage Format:

DECIMAL data type is saved using packed decimal number, i.e., COMP-3

db2 => SELECT A, HEX(A) HEX FROM TESTTAB

A      HEX  
------ ------
 12.34 01234C
-23.45 02345D

The last hex('C') means a positive value, and hex('D') means a negative value.

BTW, there is no SCALE description in data value itself, that means the value could not be interpreted itself, a COPYBOOK like definition must be provide.
In the case HEX( 01234C) could be interpreted as +1.234, +12.34, +123.4, ...

So a decimal will occupy memory bytes is the integer no less than (PRECISION + 1)/2, the "plus 1" is for SIGN; for example
DECIMAL(4, 2) => (4 + 1)/2 =>2.5 => 3
DECIMAL(5, x) => (5 + 1)/2 =>3 =>3
DECIMAL(6, 2) => (6 + 1)/2 =>3.5 => 4
DECIMAL(7, x) => (7 + 1)/2 =>4 =>4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值