新浪博客

GP 学习

2013-11-28 16:06阅读:
1. 当前日期
SELECT CURRENT_DATE;
2.当前时间
SELECT CURRENT_TIME;
3.当前日期时间
SELECT CURRENT_TIMESTAMP;
4. 除去时区
SELECT CURRENT_TIMESTAMP::TIMESTAMP(0)
SELECT CURRENT_TIMESTAMP::TIMESTAMP(0) WITHOUT TIME ZONE;
5. 计算时间差
SELECT TO_DATE (V_DATE ,'YYYY-MM-DD')-TO_DATE(V_DATE1,'YYYY-MM-DD');
6. 计算时间和
SELECT TO_DATE(V_DATE ,'YYYY-MM-DD')+12
7. 数据类型转换
(1) TEXT TO VARCHAR '':: VARCHAR
(2)
操作符 描述 例子 结果
+ 2 + 3 5
- 2 - 3 -1
*
2 * 3 6
/ 4 / 2 2
% 5 % 4 1
^ 2.0 ^ 3.0 8
|/ 平方根 |/ 25.0 5
||/ 立方根 ||/ 27.0 3
! 阶乘 5 ! 120
!! 阶乘 !! 5 120
@ 绝对值 @ -5.0 5
& 按位AND 91 & 15 11
| 按位OR 32 | 3 35
# 按位XOR 17 # 5 20
~ 按位NOT ~1 -2
<< 按位左移 1 << 4 16
>> 按位右移 8 >> 2 2
函数 返回类型
描述 例子
结果
abs(x) 绝对值 abs(-17.4) 17.4
cbrt(double) 立方根 cbrt(27.0) 3
ceil(double/numeric) 不小于参数的最小的整数 ceil(-42.8) -42
degrees(double)
把弧度转为角度 degrees(0.5) 28.6478897565412
exp(double/numeric) 自然指数 exp(1.0) 2.71828182845905
floor(double/numeric) 不大于参数的最大整数 floor(-42.8) -43
ln(double/numeric) 自然对数 ln(2.0) 0.693147180559945
log(double/numeric) 10为底的对数 log(100.0) 2
log(b numeric,x numeric) numeric指定底数的对数 log(2.0, 64.0) 6.0000000000
mod(y, x) 取余数 mod(9,4) 1
pi()
double 'π'常量 pi()
3.14159265358979
power(a double, b double) double 求a的b次幂 power(9.0, 3.0) 729
power(a numeric, b numeric) numeric 求a的b次幂 power(9.0, 3.0) 729
radians(double) double 把角度转为弧度 radians(45.0) 0.785398163397448
random() double 0.0到1.0之间的随机数值 random()
round(double/numeric) 圆整为最接近的整数 round(42.4) 42
round(v numeric, s int) numeric 圆整为s位小数数字 round(42.438,2) 42.44
sign(double/numeric) 参数的符号(-1,0,+1)
sign(-8.4) -1
sqrt(double/numeric) 平方根 sqrt(2.0) 1.4142135623731
trunc(double/numeric) 截断(向零靠近) trunc(42.8) 42
trunc(v numeric, s int) numeric 截断为s小数位置的数字 trunc(42.438,2) 42.43
函数 描述
acos(x) 反余弦
asin(x) 反正弦
atan(x) 反正切
atan2(x, y) 正切 y/x 的反函数
cos(x) 余弦
cot(x) 余切
sin(x) 正弦
tan(x) 正切
函数 返回类型 描述 例子 结果
string || string text 字串连接 'Post' || 'greSQL' PostgreSQL
bit_length(string) int 字串里二进制位的个数 bit_length('jose') 32
char_length(string) int 字串中的字符个数 char_length('jose') 4
convert(string using conversion_name) text 使用指定的转换名字改变编码。 convert('PostgreSQL' using iso_8859_1_to_utf8) 'PostgreSQL'
lower(string) text 把字串转化为小写 lower('TOM') tom
octet_length(string) int 字串中的字节数 octet_length('jose') 4
overlay(string placing string from int [for int]) text 替换子字串 overlay('Txxxxas' placing 'hom' from 2 for 4) Thomas
position(substring in string) int 指定的子字串的位置 position('om' in 'Thomas') 3
substring(string [from int] [for int]) text 抽取子字串 substring('Thomas' from 2 for 3) hom
substring(string from pattern) text 抽取匹配 POSIX 正则表达式的子字串 substring('Thomas' from '...$') mas
substring(string from pattern for escape) text 抽取匹配SQL正则表达式的子字串 substring('Thomas' from '%#'o_a#'_' for '#') oma
trim([leading | trailing | both] [characters] from string) text 从字串string的开头/结尾/两边/ 删除只包含characters(缺省是一个空白)的最长的字串 trim(both 'x' from 'xTomxx') Tom
upper(string) text 把字串转

我的更多文章

下载客户端阅读体验更佳

APP专享