openGauss学习笔记48 openGauss 高级数据管理函数

2023年 10月 12日 44.0k 0

openGauss学习笔记-48 openGauss 高级数据管理-函数48.1 数学函数48.2 三角函数列表48.3 字符串函数和操作符48.4 类型转换相关函数

openGauss学习笔记-48 openGauss 高级数据管理-函数

openGauss常用的函数如下:

48.1 数学函数

  • abs(x)

    描述:绝对值。

    返回值类型:和输入相同。

    示例:

    openGauss=# SELECT abs(-17.4);
    abs
    ------
    17.4
    (1 row)

  • cbrt(dp)

    描述:立方根。

    返回值类型:double precision

    示例:

    openGauss=# SELECT cbrt(27.0);
    cbrt
    ------
       3
    (1 row)

  • ceil(x)

    描述:不小于参数的最小的整数。

    返回值类型:整数。

    示例:

    openGauss=# SELECT ceil(-42.8);
    ceil
    ------
     -42
    (1 row)

  • degrees(dp)

    描述:把弧度转为角度。

    返回值类型:double precision

    示例:

    openGauss=# SELECT degrees(0.5);
        degrees
    ------------------
    28.6478897565412
    (1 row)

  • exp(x)

    描述:自然指数。

    返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

    示例:

    openGauss=# SELECT exp(1.0);
          exp        
    --------------------
    2.7182818284590452
    (1 row)

  • floor(x)

    描述:不大于参数的最大整数。

    返回值类型:与输入相同。

    示例:

    openGauss=# SELECT floor(-42.8);
    floor
    -------
      -43
    (1 row)

  • ln(x)

    描述:自然对数。

    返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

    示例:

    openGauss=# SELECT ln(2.0);
           ln        
    -------------------
    .6931471805599453
    (1 row)

  • log(x)

    描述:以10为底的对数。

    返回值类型:与输入相同。

    示例:

    openGauss=# SELECT log(100.0);
          log        
    --------------------
    2.0000000000000000
    (1 row)

  • log(b numeric, x numeric)

    描述:以b为底的对数。

    返回值类型:numeric

    示例:

    openGauss=# SELECT log(2.0, 64.0);
          log        
    --------------------
    6.0000000000000000
    (1 row)

  • mod(x,y)

    描述:x/y的余数(模)。如果x是0,则返回0。

    返回值类型:与参数类型相同。

    示例:

    openGauss=# SELECT mod(9,4);
    mod
    -----
      1
    (1 row)

    openGauss=# SELECT mod(9,0);
    mod
    -----
      9
    (1 row)

  • pi()

    描述:“π”常量。

    返回值类型:double precision

    示例:

    openGauss=# SELECT pi();
          pi
    ------------------
    3.14159265358979
    (1 row)

  • power(a double precision, b double precision)

    描述:a的b次幂。

    返回值类型:double precision

    示例:

    openGauss=# SELECT power(9.0, 3.0);
          power        
    ----------------------
    729.0000000000000000
    (1 row)

  • radians(dp)

    描述:把角度转为弧度。

    返回值类型:double precision

    示例:

    openGauss=# SELECT radians(45.0);
        radians
    ------------------
    .785398163397448
    (1 row)

  • random()

    描述:0.0到1.0之间的随机数。

    返回值类型:double precision

    示例:

    openGauss=# SELECT random();
        random
    ------------------
    .824823560658842
    (1 row)

  • round(x)

    描述:离输入参数最近的整数。

    返回值类型:与输入相同。

    示例:

    openGauss=# SELECT round(42.4);
    round
    -------
       42
    (1 row)

    openGauss=# SELECT round(42.6);
    round
    -------
       43
    (1 row)

  • round(v numeric, s int)

    描述:保留小数点后s位,s后一位进行四舍五入。

    返回值类型:numeric

    示例:

    openGauss=# SELECT round(42.4382, 2);
    round
    -------
    42.44
    (1 row)

  • sign(x)

    描述:输出此参数的符号。

    返回值类型:-1表示负数,0表示0,1表示正数。

    示例:

    openGauss=# SELECT sign(-8.4);
    sign
    ------
      -1
    (1 row)

  • sqrt(x)

    描述:平方根。

    返回值类型:dp or numeric,不考虑隐式类型转换的情况下与输入相同。

    示例:

    openGauss=# SELECT sqrt(2.0);
          sqrt        
    -------------------
    1.414213562373095
    (1 row)

  • trunc(x)

    描述:截断(取整数部分)。

    返回值类型:与输入相同。

    示例:

    openGauss=# SELECT trunc(42.8);
    trunc
    -------
       42
    (1 row)

  • trunc(v numeric, s int)

    描述:截断为s位小数。

    返回值类型:numeric

    示例:

    openGauss=# SELECT trunc(42.4382, 2);
    trunc
    -------
    42.43
    (1 row)

48.2 三角函数列表

  • acos(x)

    描述:反余弦。

    返回值类型:double precision

    示例:

    openGauss=# SELECT acos(-1);
          acos      
    ------------------
    3.14159265358979
    (1 row)

  • asin(x)

    描述:反正弦。

    返回值类型:double precision

    示例:

    openGauss=# SELECT asin(0.5);
          asin      
    ------------------
    .523598775598299
    (1 row)

  • atan(x)

    描述:反正切。

    返回值类型:double precision

    示例:

    openGauss=# SELECT atan(1);
          atan      
    ------------------
    .785398163397448
    (1 row)

  • atan2(y, x)

    描述:y/x的反正切。

    返回值类型:double precision

    示例:

    openGauss=# SELECT atan2(2, 1);
        atan2
    ------------------
    1.10714871779409
    (1 row)

  • cos(x)

    描述:余弦。

    返回值类型:double precision

    示例:

    openGauss=# SELECT cos(-3.1415927);
          cos        
    -------------------
    -.999999999999999
    (1 row)

  • cot(x)

    描述:余切。

    返回值类型:double precision

    示例:

    openGauss=# SELECT cot(1);
          cot
    ------------------
    .642092615934331
    (1 row)

  • sin(x)

    描述:正弦。

    返回值类型:double precision

    示例:

    openGauss=# SELECT sin(1.57079);
          sin        
    ------------------
    .999999999979986
    (1 row)

  • tan(x)

    描述:正切。

    返回值类型:double precision

    示例:

    openGauss=# SELECT tan(20);
          tan        
    ------------------
    2.23716094422474
    (1 row)

48.3 字符串函数和操作符

  • string || string

    描述:连接字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT 'MPP'||'DB' AS RESULT;
    result
    --------
    MPPDB
    (1 row)

  • bit_length(string)

    描述:字符串的位数。

    返回值类型:int

    示例:

    openGauss=# SELECT bit_length('world');
    bit_length
    ------------
            40
    (1 row)

  • convert(string bytea, src_encoding name, dest_encoding name)

    描述:以dest_encoding指定的目标编码方式转化字符串bytea。src_encoding指定源编码方式,在该编码下,string必须是合法的。

    返回值类型:bytea

    示例:

    openGauss=# SELECT convert('text_in_utf8', 'UTF8', 'GBK');
            convert        
    ----------------------------
    x746578745f696e5f75746638
    (1 row)

  • lower(string)

    描述:把字符串转化为小写。

    返回值类型:varchar

    示例:

    openGauss=# SELECT lower('TOM');
    lower
    -------
    tom
    (1 row)

  • octet_length(string)

    描述:字符串中的字节数。

    返回值类型:int

    示例:

    openGauss=# SELECT octet_length('jose');
    octet_length
    --------------
               4
    (1 row)

  • overlay(string placing string FROM int [for int])

    描述:替换子字符串。FROM int表示从第一个string的第几个字符开始替换,for int表示替换第一个string的字符数目。

    返回值类型:text

    示例:

    openGauss=# SELECT overlay('hello' placing 'world' from 2 for 3 );
    overlay
    ---------
    hworldo
    (1 row)

  • position(substring in string)

    描述:指定子字符串的位置。字符串区分大小写。

    返回值类型:int,字符串不存在时返回0。

    示例:

    openGauss=# SELECT position('ing' in 'string');
    position
    ----------
           4
    (1 row)

  • substring(string [from int] [for int])

    描述:截取子字符串,from int表示从第几个字符开始截取,for int表示截取几个字节。

    返回值类型:text

    示例:

    openGauss=# SELECT substring('Thomas' from 2 for 3);
    substring
    -----------
    hom
    (1 row)

  • substring(string from pattern)

    描述:截取匹配POSIX正则表达式的子字符串。如果没有匹配它返回空值,否则返回文本中匹配模式的那部分。

    返回值类型:text

    示例:

    openGauss=# SELECT substring('Thomas' from '...$');
    substring
    -----------
    mas
    (1 row)
    openGauss=# SELECT substring('foobar' from 'o(.)b');
    result
    --------
    o
    (1 row)
    openGauss=# SELECT substring('foobar' from '(o(.)b)');
    result
    --------
    oob
    (1 row)

  • trim([leading |trailing |both] [characters] from string)

    描述:从字符串string的开头、结尾或两边删除只包含characters中字符(缺省是一个空白)的最长的字符串。

    返回值类型:varchar

    示例:

    openGauss=# SELECT trim(BOTH 'x' FROM 'xTomxx');
    btrim
    -------
    Tom
    (1 row)

    openGauss=# SELECT trim(LEADING 'x' FROM 'xTomxx');
    ltrim
    -------
    Tomxx
    (1 row)

    openGauss=# SELECT trim(TRAILING 'x' FROM 'xTomxx');
    rtrim
    -------
    xTom
    (1 row)

  • upper(string)

    描述:把字符串转化为大写。

    返回值类型:varchar

    示例:

    openGauss=# SELECT upper('tom');
    upper
    -------
    TOM
    (1 row)

  • ascii(string)

    描述:参数string的第一个字符的ASCII码。

    返回值类型:integer

    示例:

    openGauss=# SELECT ascii('xyz');
    ascii
    -------
      120
    (1 row)

  • btrim(string text [, characters text])

    描述:从string开头和结尾删除只包含characters中字符(缺省是空白)的最长字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT btrim('sring' , 'ing');
    btrim
    -------
    sr
    (1 row)

  • chr(integer)

    描述:给出ASCII码的字符。

    返回值类型:varchar

    示例:

    openGauss=# SELECT chr(65);
    chr
    -----
    A
    (1 row)

  • convert(string bytea, src_encoding name, dest_encoding name)

    描述:以dest_encoding指定的目标编码方式转化字符串bytea。src_encoding指定源编码方式,在该编码下,string必须是合法的。

    返回值类型:bytea

    示例:

    openGauss=# SELECT convert('text_in_utf8', 'UTF8', 'GBK');
            convert        
    ----------------------------
    x746578745f696e5f75746638
    (1 row)

  • initcap(string)

    描述:将字符串中的每个单词的首字母转化为大写,其他字母转化为小写。

    返回值类型:text

    示例:

    openGauss=# SELECT initcap('hi THOMAS');
    initcap
    -----------
    Hi Thomas
    (1 row)

  • length(string)

    描述:获取参数string中字符的数目。

    返回值类型:integer

    示例:

    openGauss=# SELECT length('abcd');
    length
    --------
         4
    (1 row)

  • lpad(string text, length int [, fill text])

    描述:通过填充字符fill(缺省时为空白),把string填充为length长度。如果string已经比length长则将其尾部截断。

    返回值类型:text

    示例:

    openGauss=# SELECT lpad('hi', 5, 'xyza');
    lpad  
    -------
    xyzhi
    (1 row)

  • ltrim(string [, characters])

    描述:从字符串string的开头删除只包含characters中字符(缺省是一个空白)的最长的字符串。

    返回值类型:varchar

    示例:

    openGauss=# SELECT ltrim('xxxxTRIM','x');
    ltrim
    -------
    TRIM
    (1 row)

  • md5(string)

    描述:将string使用MD5加密,并以16进制数作为返回值。

    说明: MD5加密算法安全性低,存在安全风险,不建议使用。

    返回值类型:text

    示例:

    openGauss=# SELECT md5('ABC');
                  md5                
    ----------------------------------
    902fbdd2b1df0c4f70b4a5d23525e932
    (1 row)

  • repeat(string text, number int )

    描述:将string重复number次。

    返回值类型:text。

    示例:

    openGauss=# SELECT repeat('Pg', 4);
    repeat
    ----------
    PgPgPgPg
    (1 row)

  • replace(string text, from text, to text)

    描述:把字符串string里出现地所有子字符串from的内容替换成子字符串to的内容。

    返回值类型:text

    示例:

    openGauss=# SELECT replace('abcdefabcdef', 'cd', 'XXX');
      replace    
    ----------------
    abXXXefabXXXef
    (1 row)

  • rpad(string text, length int [, fill text])

    描述:使用填充字符fill(缺省时为空白),把string填充到length长度。如果string已经比length长则将其从尾部截断。

    返回值类型:text

    示例:

    openGauss=# SELECT rpad('hi', 5, 'xy');
    rpad
    -------
    hixyx
    (1 row)

  • rtrim(string text [, characters text])

    描述:从字符串string的结尾删除只包含characters中字符(缺省是个空白)的最长的字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT rtrim('trimxxxx', 'x');
    rtrim
    -------
    trim
    (1 row)

  • split_part(string text, delimiter text, field int)

    描述:根据delimiter分隔string返回生成的第field个子字符串(从出现第一个delimiter的text为基础)。

    返回值类型:text

    示例:

    openGauss=# SELECT split_part('abc~@~def~@~ghi', '~@~', 2);
    split_part
    ------------
    def
    (1 row)

  • strpos(string, substring)

    描述:指定的子字符串的位置。和position(substring in string)一样,不过参数顺序相反。

    返回值类型:int

    示例:

    openGauss=# SELECT strpos('source', 'rc');
    strpos
    --------
         4
    (1 row)

  • to_hex(number int or bigint)

    描述:把number转换成十六进制表现形式。

    返回值类型:text

    示例:

    openGauss=# SELECT to_hex(2147483647);
    to_hex
    ----------
    7fffffff
    (1 row)

  • translate(string text, from text, to text)

    描述:把在string中包含的任何匹配from中字符的字符转化为对应的在to中的字符。如果from比to长,删掉在from中出现的额外的字符。

    返回值类型:text

    示例:

    openGauss=# SELECT translate('12345', '143', 'ax');
    translate
    -----------
    a2x5
    (1 row)

48.4 类型转换相关函数

  • to_char(timestamp, text)

    描述:将时间戳类型的值转换为指定格式的字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT to_char(current_timestamp, 'HH12:MI:SS');
    to_char
    ----------
    10:55:59
    (1 row)

  • to_char(interval, text)

    描述:将时间间隔类型的值转换为指定格式的字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS');
    to_char
    ----------
    15:02:12
    (1 row)

  • to_char(int, text)

    描述:将整数类型的值转换为指定格式的字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT to_char(125, '999');
    to_char
    ---------
     125
    (1 row)

  • to_char(double precision/real, text)

    描述:将浮点类型的值转换为指定格式的字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT to_char(125.8::real, '999D99');
    to_char
    ---------
     125.80
    (1 row)

  • to_char(numeric, text)

    描述:将数字类型的值转换为指定格式的字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT to_char(-125.8, '999D99S');
    to_char
    ---------
    125.80-
    (1 row)

  • to_date(text, text)

    描述:将字符串类型的值转换为指定格式的日期。

    返回值类型:timestamp without time zone

    示例:

    openGauss=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY');
          to_date
    ---------------------
    2000-12-05 00:00:00
    (1 row)

  • to_number(text, text)

    描述:将字符串类型的值转换为指定格式的数字。

    返回值类型:numeric

    示例:

    openGauss=# SELECT to_number('12,454.8-', '99G999D9S');
    to_number
    -----------
     -12454.8
    (1 row)

  • to_timestamp(text, text)

    描述:将字符串类型的值转换为指定格式的时间戳。

    返回值类型:timestamp

    示例:

    openGauss=# SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY');
      to_timestamp
    ---------------------
    2000-12-05 00:00:00
    (1 row)

  • to_timestamp(double precision)

    描述:把UNIX纪元转换成时间戳。

    返回值类型:timestamp with time zone

    示例:

    openGauss=# SELECT to_timestamp(1284352323);
        to_timestamp      
    ------------------------
    2010-09-13 12:32:03+08
    (1 row)

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论