打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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)


--5 附: PostgreSQL 类型转换函数

FunctionReturn TypeDescriptionExample


to_char
(timestamp, text

)


text
convert time stamp to string
to_char(current_timestamp, 'HH12:MI:SS')

to_char
(
interval, text
)

text
convert interval to string
to_char(interval '15h 2m 12s', 'HH24:MI:SS')

to_char
(
int, text
)

text
convert integer to string
to_char(125, '999')


to_char
(
double

precision
, text
)


text
convert real/double precision to string
to_char(125.8::real, '999D9')

to_char
(
numeric, text
)

text
convert numeric to string
to_char(-125.8, '999D99S')

to_date
(text, text
)

date
convert string to date
to_date('05 Dec 2000', 'DD Mon YYYY')

to_number
(
text, text
)

numeric
convert string to numeric
to_number('12,454.8-', '99G999D9S')

to_timestamp
(
text, text
)

timestamp with time zone
convert string to time stamp
to_timestamp('05 Dec 2000', 'DD Mon YYYY')

to_timestamp
(
double precision
)

timestamp with time zone
convert Unix epoch to time stamp
to_timestamp(1284352323)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
PostgresQL 学习记录之与oracle区别(一)
postgresql常用函数及操作符及类型转换等
Oracle to_char格式化函数 -
PostgreSQL 常用函数
5 Powerful Excel Functions That Make Work Easier
构建AI前的数据准备,SQL要比Python强
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服