暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

函数 lpad、rpad 不支持 length 函数运算改写

路易十四猪 2022-06-30
404

问题现象 GBase 8a MPP Cluster FAQ FAQ
文档版本(2021-04-02) 南大通用数据技术股份有限公司
74
函数 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> 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 8a MPP Cluster FAQ FAQ
文档版本(2021-04-02) 南大通用数据技术股份有限公司
75
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> 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. 最终采用的方法是自定义一个函数取 * 个数再与显示字符拼接的方法解决此问
题: GBase 8a MPP Cluster FAQ FAQ
文档版本(2021-04-02) 南大通用数据技术股份有限公司
76
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> 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********* | 赵** |
————————————————
版权声明:本文为CSDN博主「m0_49291452」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_49291452/article/details/125541191

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论