openGauss SQL参考—函数和操作符:字符处理函数和操作符(5)

  • substrb(text,int,int)

    描述:提取子字符串,第一个int表示提取的起始位置,第二个表示提取几位字符。

    返回值类型:text

    示例:

    openGauss=# SELECT substrb('string',2,3);
     substrb
    ---------
     tri
    (1 row)
    
  • substrb(text,int)

    描述:提取子字符串,int表示提取的起始位置。

    返回值类型:text

    示例:

    openGauss=# SELECT substrb('string',2);
     substrb
    ---------
     tring
    (1 row)
    
  • substr(bytea,from,count)

    描述:从参数bytea中抽取子字符串。from表示抽取的起始位置,count表示抽取的子字符串长度。

    返回值类型:text

    示例:

    openGauss=# SELECT substr('string',2,3);
     substr
    --------
     tri
    (1 row)
    
  • string || string

    描述:连接字符串。

    返回值类型:text

    示例:

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

    描述:连接字符串和非字符串。

    返回值类型:text

    示例:

    openGauss=# SELECT 'Value: '||42 AS RESULT;
      result   
    -----------
     Value: 42
    (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)
    
  • length(string)

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

    返回值类型:integer

    示例:

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

    描述:获取参数string中字节的数目。与字符集有关,同样的中文字符,在GBK与UTF8中,返回的字节数不同。

    返回值类型:integer

    示例:

    openGauss=# SELECT lengthb('Chinese');
     lengthb 
    ---------
           7
    (1 row)
    
  • substr(string,from)

    描述:

    从参数string中抽取子字符串。

    from表示抽取的起始位置。

    • from为0时,按1处理。
    • from为正数时,抽取从from到末尾的所有字符。
    • from为负数时,抽取字符串的后n个字符,n为from的绝对值。

    返回值类型:varchar

    示例:

    from为正数时:

    openGauss=# SELECT substr('ABCDEF',2);
     substr
    --------
     BCDEF
    (1 row)
    

    from为负数时:

    openGauss=# SELECT substr('ABCDEF',-2);
     substr
    --------
     EF
    (1 row)
    
  • substr(string,from,count)

    描述:

    从参数string中抽取子字符串。

    from表示抽取的起始位置。

    count表示抽取的子字符串长度。

    • from为0时,按1处理。
    • from为正数时,抽取从from开始的count个字符。
    • from为负数时,抽取从倒数第n个开始的count个字符,n为from的绝对值。
    • count小于1时,返回null。

    返回值类型:varchar

    示例:

    from为正数时:

    openGauss=# SELECT substr('ABCDEF',2,2);
     substr 
    --------
     BC
    (1 row)
    

    from为负数时:

    openGauss=# SELECT substr('ABCDEF',-3,2);
     substr 
    --------
     DE
    (1 row)
    
  • substrb(string,from)

    描述:该函数和SUBSTR(string,from)函数功能一致,但是计算单位为字节。

    返回值类型:bytea

    示例:

    openGauss=# SELECT substrb('ABCDEF',-2);
     substrb 
    ---------
     EF
    (1 row)
posted @ 2024-07-30 10:36  openGauss-bot  阅读(72)  评论(0)    收藏  举报