DB2取浮点数的小数部分


一、最简单,最快捷的方法

各种DB2版本通吃,使用floor函数、或cast函数取整数部分,然后用原来的数减去整数部分即可:

DB2 for i上面的查询:

select 123.556 - floor(123.556) from qsys2/qsqptabl           
....+....1....+...             
Numeric Expression             
            .556               
********  End of data  ********

DB2 V10.1 for luw上面的查询:

select 123.99-cast(123.99 as int),100.1-floor(100.1) from sysibm.sysdummy1
1                2
---------------- --------
            0.99      0.1

注意:这里得到的数据直接为浮点数,可以进行数学运算

二、浮点数长度固定、小数位数固定,但实际存储数值不确定的,如下处理:

DB2 for i(AS400)上面的查询

1.使用locate定位转换为字符串后的小数点,然后再根据此小数点,接取整数和小数部分:

select substr(char(123.99),locate('.',char(123.99))+1),char(123.99)
 from qsys2/qsqptabl                                                                     
....+....1....+....2....       
SUBSTR  CHAR ( 123.99 )       
99          123.99           
********  End of data  ********

2.使用position或者posstr函数定位小数点的位置

还是用上面的数据,得到如下的结果:

select char(123.99),                                       
      substr(char(123.99),position('.' in char(123.99))+1),
      substr(char(123.99),posstr(char(123.99),'.')+1)     
  from qsys2/qsqptabl                                     
....+....1....+....2....+....3...
CHAR ( 123.99 )  SUBSTR  SUBSTR
    123.99      99      99   
********  End of data  ******** 

DB2 V10.1 for luw下面的查询:

select char(123.99),
      substr(char(123.99),
      position('.' in char(123.99) using octets)+1),
      substr(char(123.99),posstr(char(123.99),'.')+1)
  from sysibm.sysdummy1
1      2      3
------- ------- -------
123.99  99      99

  • 1
  • 2
  • 下一页

相关内容