函数说明
to_single_byte (arg)
to_single_byte: 全角转半角的函数名;
arg: to_single_byte 函数的参数, 可以为任何类型的值和列。
如果 arg 为字符串,并且字符串里面含有全角的话,在输出结果中就会将全角字符转为半角字符, 其他字符保持不变。
该函数仅在 UTF8 字符集和 GBK 字符集下有效。
- 当前仅存在 95 个字符,支持全角转半角。
95 个字符如下:
空格, ! , ", #, $, %, &,‗, ( , ) , *, +,, , - , . , /,
0-9, : , ; , <, =,>,? , @, A-Z, [,,],^,_,‗,a-z,{, |,},~ - 函数的结果类型:是 STRING_RESULT
class Item_func_to_single_byte :public Item_str_func
enum Item_result result_type () const { return STRING_RESULT; }
create as select 时 候 , 函 数 的 field_type 根 据 result_type 来 确 定 , 见
Item.field_type 函数, 如果是 result_type 为 STRING_RESULT 的时候,会根
据 max_length 来判断是 varchar, longblob, medium_blob。
注意
只有 varchar、 char、 text,支持字符串类型的列类型支持全角字符, 并且使用
to_single_byte 转换成功。
Longblob、 blob 虽然能存放全角字符,但是是按二进制存储的, to_single_byte
转换后还是全角字符。
示例
- 示例:
create table t(a int, b varchar(10), c datetime, t text, e longblob, f blob, g char(10));
gbase> insert into t values(1, 'aaaaaa ', '2011-01-01 11:11:11', 'aaaa ', 'aaaa ', 'aaaa ', 'aaaa ');
Query OK, 1 row affected (Elapsed: 00:00:00.05)
gbase> select to_single_byte(a),to_single_byte(b), to_single_byte(c), to_single_byte(t), to_single_byte(e),
to_single_byte(f), to_single_byte(g) from t;
+-------------------+-------------------+---------------------+-------------------+-------------------+-------------------+------------------
-+
| to_single_byte(a) | to_single_byte(b) | to_single_byte(c) | to_single_byte(t) | to_single_byte(e) | to_single_byte(f) |
to_single_byte(g) |
+-------------------+-------------------+---------------------+-------------------+-------------------+-------------------+------------------
-+
| 1 | aaaaaa | 2011-01-01 11:11:11 | aaaa | aaaa | aaaa | aaaa |
+-------------------+-------------------+---------------------+-------------------+-------------------+-------------------+------------------
-+
1 row in set (Elapsed: 00:00:00.03)
gbase> create table ty as select to_single_byte(a),to_single_byte(b), to_single_byte(c), to_single_byte(t),
to_single_byte(e), to_single_byte(f), to_single_byte(g) from t;
Query OK, 1 row affected (Elapsed: 00:00:00.08)
Records: 1 Duplicates: 0 Warnings: 0
gbase> show create table ty;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
| +
-------+----------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------+
| ty | CREATE TABLE "ty" (
"to_single_byte(a)" varchar(11) DEFAULT NULL,
"to_single_byte(b)" varchar(10) DEFAULT NULL,
"to_single_byte(c)" varchar(26) DEFAULT NULL,
"to_single_byte(t)" varchar(10922) DEFAULT NULL,
"to_single_byte(e)" longblob,
"to_single_byte(f)" varbinary(32767) DEFAULT NULL,
"to_single_byte(g)" varchar(10) DEFAULT NULL
) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
gbase> select * from ty;
+-------------------+-------------------+---------------------+-------------------+-------------------+-------------------+-------------------+
| to_single_byte(a) | to_single_byte(b) | to_single_byte(c) | to_single_byte(t) | to_single_byte(e) | to_single_byte(f) |
to_single_byte(g) |
+-------------------+-------------------+---------------------+-------------------+-------------------+-------------------+-------------------+
| 1 | aaaaaa | 2011-01-01 11:11:11 | aaaa | aaaa | aaaa | aaaa |
+-------------------+-------------------+---------------------+-------------------+-------------------+-------------------+-------------------+
1 row in set (Elapsed: 00:00:00.04)