首页 > 数据库技术 > 详细

PostgreSQL 类型转换 -除法

时间:2016-04-28 12:03:52      阅读:180      评论:0      收藏:0      [点我收藏+]
PostgreSQL 类型转换  

  类似Oracle ,PostgreSQL也有强大的类型转换函数, 下面仅举两个类型转换例子。

      
--1 例子
postgres=# select 1/4;
 ?column? 
----------
        0
(1 row)

        在PG里如果想做除法并想保留小数,用上面的方法却行不通,因为"/" 运算结果为取整,并
且会截掉小数部分。
 

--2 类型转换
postgres=# select round(1::numeric/4::numeric,2);
 round 
-------
  0.25
(1 row)

  备注:类型转换后,就能保留小数部分了。


--3 也可以通过 cast 函数进行转换
postgres=# select round( cast ( 1 as numeric )/ cast( 4 as numeric),2);
 round 
-------
  0.25
(1 row)


--4 关于 cast 函数的用法
postgres=# SELECT substr(CAST (1234 AS text), 3,1);
 substr 
--------
 3
(1 row)

PostgreSQL 类型转换 -除法

原文:http://www.cnblogs.com/Amos-Turing/p/5441875.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!