postgres 类型转换 cast 转

转自: http://blog.csdn.net/yufenghyc/article/details/45869509函数

 

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

        在PG里若是想作除法并想保留小数,用上面的方法却行不通,由于"/" 运算结果为取整,而且会截掉小数部分。
 .net

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

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


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


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

相关文章
相关标签/搜索