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

问题现象 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********* | 赵** |
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值