问题现象 函数lpad、rpad 不支持length 函数运算改写。 解决方法 业务要求1:保持9 位,不足位数前面补0。 如:1234,结果:000001234。 select lpad(GH_FLAG,9-length(GH_FLAG),'0') from ics.ASSE_BILL_ITEM_RESULT_4; 目前不支持这种语法,改写为: select left('000000000',9-length(GH_FLAG))||GH_FLAG from ics.ASSE_BILL_ITEM_RESULT_4; 业务要求2:只显示手机号前两位和姓名的第一个字符,其它以*代替。 测试表: insert into t_01 values('13512345678','赵子龙'); 1. 客户使用的是length(phone)-1,这里用户理解有误,以为是需要补 length(phone)-1 个*,length 函数后有-1,直接报错了:
GBase 8a MPP Cluster 最佳实践 5 FAQ 文档版本(2022-02-11) 南大通用数据技术股份有限公司 111 gbase> select rpad(left(phone,7),length(phone)-1,'*') as phone from t_01; ERROR 1708 (HY000): [192.168.3.26:5050](GBA-02EX-0005) Failed to query in gnode: DETAIL: Query failed. CAUSE:(GBA-01EX-700) The query includes syntax that is not supported by the Express engine. Either restructure the query with supported syntax, or enable the GBase Query Path in the configuration file to execute the query with reduced performance. SQL: SELECT /*192.168.3.25_13_32_2017-02-26_22:24:31*/ /*+ TID('79') */ rpad(left(`testdb.t_01`.`phone`, 7), (length(`testdb.t_01`.`phone`) - 1), '*') AS `phone` FROM `testdb`.` 2. 将-1 去除,汉字后的*显示个数也不正确: gbase> select phone,name, -> rpad(left(phone,7),length(phone),'*') as phone, -> rpad(left(name,1),length(name),'*') as name -> from t_01; +-------------+-----------+-------------+-------------+ | phone | name | phone | name | +-------------+-----------+-------------+-------------+ | 13512345678 | 赵子龙 | 1351234**** | 赵******** | +-------------+-----------+-------------+-------------+ Phone:后4 位为*,正确。 name:后2 位应为*,因为length 取的是字节数所以变成了8 个*。 3. 改为character_length,直接报错了,按研发的解释,目前rpad 函数中不支持 使用character_length。
GBase 8a MPP Cluster 最佳实践 5 FAQ 文档版本(2022-02-11) 南大通用数据技术股份有限公司 112 gbase> select rpad(left(name,1),character_length(name),'*') as name from t_01; ERROR 1708 (HY000): [192.168.3.26:5050](GBA-02EX-0005) Failed to query in gnode: DETAIL: Query failed. CAUSE:(GBA-01EX-700) The query includes syntax that is not supported by the Express engine. Either restructure the query with supported syntax, or enable the GBase Query Path in the configuration file to execute the query with reduced performance. SQL: SELECT /*192.168.3.25_13_26_2017-02-26_22:20:13*/ /*+ TID('76') */ rpad(left(`testdb.t_01`.`name`, 1), char_length(`testdb.t_01`.`name`), '*') AS `name` FROM `testdb`.`t_01_n 4. 最终采用的方法是自定义一个函数取*个数再与显示字符拼接的方法解决此 问题: delimiter $$ drop function if exists testdb.f_getstar $$ CREATE FUNCTION testdb.f_getstar(n INT) RETURNS VARCHAR(100) BEGIN DECLARE i INT DEFAULT 0; DECLARE s VARCHAR(100) DEFAULT ''; myloop: LOOP SET i = i+1; SET s = CONCAT(s, '*'); IF i>=n THEN LEAVE myloop; END IF; END LOOP myloop; RETURN s; END$$ delimiter ; 5. 测试结果:
GBase 8a MPP Cluster 最佳实践 5 FAQ 文档版本(2022-02-11) 南大通用数据技术股份有限公司 113 gbase> select phone,name, -> left(a.phone,2)||testdb.f_getstar(length(a.phone)-2) as phone, -> left(a.name,1)||testdb.f_getstar(CHARACTER_LENGTH(a.name)-1) as name -> from testdb.t_01 a ; +-------------+-----------+-------------+-------+ | phone | name | phone | name | +-------------+-----------+-------------+-------+ | 13512345678 | 赵子龙 | 13********* | 赵** |