• 函数 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********* | 赵** |
  • 相关阅读:
    GBase 8s数据库DB-Access全屏菜单界面介绍(4)
    契约测试理论篇
    webpack中loader和plugin的区别
    Java的方法和递归
    卧式铣床升降台主传动系统设计(说明书+翻译及原文+cad图纸+proe三维图纸)
    基于Python+Django的热门旅游景点数据分析系统的设计与实现(源码+lw+部署文档+讲解等)
    概率论的学习和整理--番外9:关于贝特朗悖论的学习
    电机控制从入门到吹牛
    golang实现远程控制主机
    【Java】集合框架和泛型(二)
  • 原文地址:https://blog.csdn.net/m0_49291452/article/details/125541191