函数类型
函数的类型可以分为两种:单行函数和多行函数
1、单行函数
1.1 数值型函数
常用的数值型函数有:
Round() 按指定精度对十进制数四舍五入
Sin() 求正弦三角函数
Cos() 求余弦三角函数
Abs() 求绝对值函数
Sqrt() 求平方根函数
mod() 求除法余数
power() 求幂函数
floor() 求小于等于某数的最大整数
ceil() 求大于等于某数的最小整数
Trunc() 向零的方向截断浮点数
highgo=# select round(45.923,2),round(45.923,0),round(45.923,-1);
round | round | round
-------+-------+-------
45.92 | 46 | 50
(1 row)
1.2字符串函数
#小写转换函数:
LOWER :将字符串中大写转换成小写
UPPER :将字符串中小写转换成大写
INITCAP :将单词中首个字母转换成大写
highgo=# select id,initcap(name) from test1;
id | initcap
----+---------
1 | Adam
2 | Lilith
(2 rows)
#字符操作函数
SUBSTR() : 取字符串
LENGTH() : 求字符串的长度
LPAD() : 左补指定的长度
RPAD() : 右补指定的长度
REPLACE() : 替换字符串
TRIM():从左边或者右边删除指定字符串
highgo=# select substr('asdfg',2,3);
substr
--------
sdf
(1 row)
highgo=# select length('asdfg');
length
--------
5
(1 row)
highgo=# select position('w' in 'qwert');
position
----------
2
(1 row)
highgo=# select lpad('asdf',10,'*');
lpad
------------
******asdf
(1 row)
highgo=# select rpad('asdf',10,'*');
rpad
------------
asdf******
(1 row)
highgo=# select replace('asdfg asdfg','d','w');
replace
-------------
aswfg aswfg
(1 row)
highgo=# select trim('ha' from 'hasdfg');
btrim
-------
sdfg
(1 row)
1.3时间日期函数
Abstime()取当前的时间
注意:该函数与 oracle 中 sysdate 函数对应。Oracle 里面的用法:select sysdate from dual ;
highgo=# select abstime(timestamp 'now');
abstime
------------------------
2017-04-01 08:28:29+08
(1 row)
1.4格式化函数
格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,int,float,numeric) 转换成格式化的字符串以及反过来从格式化的字符串转换成原始的数据类型。 To_char()将时间 int float numeic 其中的之一的转换成字符串
To_date()将字符串转换成日期
To_number()将字符串转换成numeric 类型的实数。
highgo=# select abstime(timestamp 'now');
abstime
------------------------
2017-04-01 08:28:29+08
(1 row)
highgo=# select to_char(234,'999');
to_char
---------
234
(1 row)
highgo=# select to_date('05 dec 2011','dd mon yyyy');
to_date
------------
2011-12-05
(1 row)
highgo=# select to_number('12,454.8$','99G999D9S');
to_number
-----------
12454.8
(1 row)
2、多行函数
多行函数有:
AVG() 求平均值
SUM() 求总和
MIN() 求最小值
MAX() 求最大值
COUNT() 统计总记录数
pg学习_函数类型
最新推荐文章于 2024-06-13 11:40:10 发布