南大通用GBase 8a字符串函数bit_length的用法和例子

本文介绍GBase 8a数据库集群的bit_length函数的语法、使用说明和例子。

语法

BIT_LENGTH(str)

说明

返回字符串 str 的比特长度,以比特进行计算。

注意必须是字符串,如果你输入的是数字,会先转换成对应的字符串,比如10,他会变成'10'来处理,而不是数字10。

空字符串返回长度0

null值返回 null.

样例

字符串ASCII

每个ASCII字符占用8位。空字符串返回0.

gbase> select bit_length('');
+----------------+
| bit_length('') |
+----------------+
|              0 |
+----------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length('A');
+-----------------+
| bit_length('A') |
+-----------------+
|               8 |
+-----------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length('AB');
+------------------+
| bit_length('AB') |
+------------------+
|               16 |
+------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length('ABC');
+-------------------+
| bit_length('ABC') |
+-------------------+
|                24 |
+-------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length('ABCD');
+--------------------+
| bit_length('ABCD') |
+--------------------+
|                 32 |
+--------------------+
1 row in set (Elapsed: 00:00:00.00)

汉字

UTF8编码

每个占用3个字节24位。

gbase> select bit_length('中');
+-------------------+
| bit_length('中')  |
+-------------------+
|                24 |
+-------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length('中国');
+----------------------+
| bit_length('中国')   |
+----------------------+
|                   48 |
+----------------------+
1 row in set (Elapsed: 00:00:00.00)

GBK编码

每个字符占用2个字节,16位。

gbase> create table t_gbk(name varchar(100))default charset=gbk;
Query OK, 0 rows affected (Elapsed: 00:00:00.42)

gbase> insert into t_gbk values('中'),('中国');
Query OK, 2 rows affected (Elapsed: 00:00:00.27)
Records: 2  Duplicates: 0  Warnings: 0

gbase> select name,bit_length(name) from t_gbk
    -> ;
+--------+------------------+
| name   | bit_length(name) |
+--------+------------------+
| 中     |               16 |
| 中国   |               32 |
+--------+------------------+
2 rows in set (Elapsed: 00:00:00.01)

数字

直接数字

参数直接写数字,会被先转化成对应的字符串。与预期不符。如下数字10对应0xA只占用8位,但10变成了'10' 则占用了16位。

gbase> select bit_length(1);
+---------------+
| bit_length(1) |
+---------------+
|             8 |
+---------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length(10);
+----------------+
| bit_length(10) |
+----------------+
|             16 |
+----------------+
1 row in set (Elapsed: 00:00:00.01)

用hex转换成数字

用hex避免了字符串的转换,而是改成了16进制数,其长度符合预期。

gbase> select hex(10),bit_length(hex(10));
+---------+---------------------+
| hex(10) | bit_length(hex(10)) |
+---------+---------------------+
| A       |                   8 |
+---------+---------------------+
1 row in set (Elapsed: 00:00:00.00)
gbase> select hex(100),bit_length(hex(100));
+----------+----------------------+
| hex(100) | bit_length(hex(100)) |
+----------+----------------------+
| 64       |                   16 |
+----------+----------------------+
1 row in set (Elapsed: 00:00:00.00)
gbase> select hex(257),bit_length(hex(257));
+----------+----------------------+
| hex(257) | bit_length(hex(257)) |
+----------+----------------------+
| 101      |                   24 |
+----------+----------------------+
1 row in set (Elapsed: 00:00:00.00)

16进制数

直接用16进制表达形式,可以获得正确的结果。

gbase> select bit_length(x'AA');
+-------------------+
| bit_length(x'AA') |
+-------------------+
|                 8 |
+-------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select bit_length(x'AABB');
+---------------------+
| bit_length(x'AABB') |
+---------------------+
|                  16 |
+---------------------+

NULL值

gbase> select bit_length(null);
+------------------+
| bit_length(null) |
+------------------+
|             NULL |
+------------------+
1 row in set (Elapsed: 00:00:00.00)

参考