164 Star 224 Fork 1.2K

openGauss / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
位串函数和操作符.md 2.91 KB
一键复制 编辑 原始数据 按行查看 历史
liyang 提交于 2020-12-08 17:49 . fix bugs

位串函数和操作符

位串操作符

除了常用的比较操作符之外,还可以使用以下的操作符。&,|和#的位串操作数必须等长。在位移的时候,保留原始的位串长度(并以0填充)。

  • ||

    描述:位串之间进行连接。

    示例:

    postgres=# SELECT B'10001' || B'011' AS RESULT;
      result
    ----------
     10001011
    (1 row)

    说明:
    单字段内部连续连接操作不建议超过180次,若超过需拆分为多个连续连接的字符串,它们之间再做连接操作。
    例如:str1||str2||str3||str4 拆分为 (str1||str2)||(str3||str4)。

  • &

    描述:位串之间进行“与”操作。

    示例:

    postgres=# SELECT B'10001' & B'01101' AS RESULT;
     result 
    --------
     00001
    (1 row)
  • |

    描述:位串之间进行“或”操作。

    示例:

    postgres=# SELECT B'10001' | B'01101' AS RESULT;
     result 
    --------
     11101
    (1 row)
  • #

    描述:位串之间如果不一致进行“或”操作。如果两个位串中对应位置都为1或者0,则该位置返回为0。

    示例:

    postgres=# SELECT B'10001' # B'01101' AS RESULT;
     result 
    --------
     11100
    (1 row)
  • ~

    描述:位串之间进行“非”操作。

    示例:

    postgres=# SELECT ~B'10001'AS RESULT;
     result  
    ----------
     01110
    (1 row)
  • <<

    描述:位串进行左移操作。

    示例:

    postgres=# SELECT B'10001' << 3 AS RESULT;
     result  
    ----------
     01000
    (1 row)
  • >>

    描述:位串进行右移操作。

    示例:

    postgres=# SELECT B'10001' >> 2 AS RESULT;
     result  
    ----------
     00100
    (1 row)

下面的SQL标准函数除了可以用于字符串之外,也可以用于位串:length,bit_length,octet_length,position,substring,overlay。

下面的函数用于位串和二进制字符串:get_bit,set_bit。当用于位串时,这些函数位数从字符串的第一位(最左边)作为0位。

另外,可以在整数和bit之间来回转换。示例:

postgres=# SELECT 44::bit(10) AS RESULT;
   result
------------
 0000101100
(1 row)

postgres=# SELECT 44::bit(3) AS RESULT;
 result 
--------
 100
(1 row)

postgres=# SELECT cast(-44 as bit(12)) AS RESULT;
    result    
--------------
 111111010100
(1 row)

postgres=# SELECT '1110'::bit(4)::integer AS RESULT;
 result 
--------
     14
(1 row)

说明:
只是转换为“bit”的意思是转换成bit(1),因此只会转换成整数的最低位。

1
https://gitee.com/opengauss/docs.git
git@gitee.com:opengauss/docs.git
opengauss
docs
docs
2.0.0

搜索帮助