博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
postgreSQL 类型转换
阅读量:4116 次
发布时间:2019-05-25

本文共 1447 字,大约阅读时间需要 4 分钟。

  类似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)

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

Function Return Type Description Example

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

转载地址:http://vsupi.baihongyu.com/

你可能感兴趣的文章
JavaSE_day_03 方法
查看>>
day-03JavaSE_循环
查看>>
Mysql初始化的命令
查看>>
day_21_0817_Mysql
查看>>
day-22 mysql_SQL 结构化查询语言
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
HTML&CSS进阶
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
MySQL-分布式架构-MyCAT
查看>>
设计模式六大原则(6):开闭原则
查看>>
阿里面试总结--JAVA
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>