zoukankan      html  css  js  c++  java
  • PostgreSQL 常用函数

    PostgreSQL 常用函数

    PostgreSQL内置函数也称为聚合函数,用于对字符串或数字数据执行处理。

    下面是所有通用PostgreSQL内置函数的列表:

    • COUNT 函数:用于计算数据库表中的行数。
    • MAX 函数:用于查询某一特定列中最大值。
    • MIN 函数:用于查询某一特定列中最小值。
    • AVG 函数:用于计算某一特定列中平均值。
    • SUM 函数:用于计算数字列所有值的总和。
    • ARRAY 函数:用于输入值(包括null)添加到数组中。
    • Numeric 函数:完整列出一个SQL中所需的操作数的函数。
    • String 函数:完整列出一个SQL中所需的操作字符的函数。

    数字函数

    需要说明的是,这些函数中有许多都存在多种形式,区别只是参数类型不同。除非特别指明,任何特定形式的函数都返回和他的参数相同的数据类型。

    函数 返回类型 描述 例子 结果
    abs()   绝对值 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)   自然对数 In(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 "TT"常量 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

     

    字符串函数和操作符

    下面是PostgreSQL中提供的字符串操作符列表

    函数 返回类型 说明 例子 结果
     string || string  text 字串连接  'Post' || 'greSQL'  PostgreSQL
     bit_length(string)  int 字串里二进制位的个数  bit_length('netstars')  64
     char_length(string)  int 字串中的字符个数  char_length('netstars')  8
     lower(string)  text 使用指定的转换名字改变编码。   lower('NETSTARS')  netstars
     octet_length(string)  int 字串中的字节数  octet_length('netstars')  8
     overlay(string placing string from int for int)  text 替换子字串  overlay('XXXstars' placing 'net' from 1 for 3)  netstars
     position(substring in 'string')  int 指定的子字串的位置  position('st' in 'netstars')  4
     substring(string from int for int)  text 抽取子字串  substring('netstars' from 4 for 8)  stars
     upper(string)  text 把字串转化为大写。  upper('netstars')  NETSTARS
     ascii(text)  int 参数第一个字符的ASCII码  ascii('n')  110
     chr(int)  text 给出ASCII码的字符  chr(int)  A
     select initcap(string)  text 把每个单词的第一个子母转为大写,其它的保留小写。单词是一系列字母数字组成的字符,用非字母数字分隔。  select initcap('net stars')  
     length(string text)  int string中字符的数目  length('netstars core')  13
     repeat(string text, number int)  text 重复string number次  repeat('net',  3)  netnetnet

     


    类型转换相关函数

    函数返回类型描述实例
    to_char(timestamp, text) text 将时间戳转换为字符串 to_char(current_timestamp, 'HH12:MI:SS')
    to_char(interval, text) text 将时间间隔转换为字符串 to_char(interval '15h 2m 12s', 'HH24:MI:SS')
    to_char(int, text) text 整型转换为字符串 to_char(125, '999')
    to_char(double precision, text) text 双精度转换为字符串 to_char(125.8::real, '999D9')
    to_char(numeric, text) text 数字转换为字符串 to_char(-125.8, '999D99S')
    to_date(text, text) date 字符串转换为日期 to_date('05 Dec 2000', 'DD Mon YYYY')
    to_number(text, text) numeric 转换字符串为数字 to_number('12,454.8-', '99G999D9S')
    to_timestamp(text, text) timestamp 转换为指定的时间格式 time zone convert string to time stamp to_timestamp('05 Dec 2000', 'DD Mon YYYY')
    to_timestamp(double precision) timestamp 把UNIX纪元转换成时间戳 to_timestamp(1284352323)

     

  • 相关阅读:
    数据结构小总结(成都磨子桥技工学校数据结构前12题)
    Scrum 冲刺博客第二篇
    Scrum 冲刺博客第一篇
    centos部署keepalived服务
    第四周作业
    Svelte 中怎样做双向数据绑定
    Svelte 中多层组件事件转发
    Svelte 中的事件修饰符
    怎样在 Svelte 中设置自定义事件
    怎样使用 Svelte 中的异步块
  • 原文地址:https://www.cnblogs.com/ljknlb/p/11650862.html
Copyright © 2011-2022 走看看