• mysql字段中提取汉字,去除数字以及字母


    如果只是删除尾部的中文,保留数据,可以用以下的简单方式

    MySQL [test]> select '1234万美元' + 0 as num;
    +------+
    | num  |
    +------+
    | 1234 |
    +------+
    DELIMITER $$
    DROP FUNCTION IF EXISTS `Num_char_extract`$$
    CREATE FUNCTION `Num_char_extract`(Varstring VARCHAR(100)CHARSET utf8, flag INT) RETURNS VARCHAR(50) CHARSET utf8
    COMMENT '标识 0 提取数字 1 提取字母 2提取数字+字母 3 提取汉字'
    BEGIN
        DECLARE len INT DEFAULT 0;
        DECLARE Tmp VARCHAR(100) DEFAULT '';
        SET len=CHAR_LENGTH(Varstring);
        IF flag = 0
        THEN
            WHILE len > 0 DO
            IF MID(Varstring,len,1)REGEXP'[0-9]' THEN
            SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
            END IF;
            SET len = len - 1;
            END WHILE;
        ELSEIF flag=1
        THEN
            WHILE len > 0 DO
            IF (MID(Varstring,len,1)REGEXP '[a-zA-Z]')
            THEN
            SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
            END IF;
            SET len = len - 1;
            END WHILE;
        ELSEIF flag=2
        THEN
            WHILE len > 0 DO
            IF ( (MID(Varstring,len,1)REGEXP'[0-9]')
            OR (MID(Varstring,len,1)REGEXP '[a-zA-Z]') )
            THEN
            SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
            END IF;
            SET len = len - 1;
            END WHILE;
        ELSEIF flag=3
        THEN
            WHILE len > 0 DO
            IF NOT (MID(Varstring,len,1)REGEXP '^[u0391-uFFE5]')
            THEN
            SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
            END IF;
            SET len = len - 1;
            END WHILE;
        ELSE
            SET Tmp = 'Error: The second paramter should be in (0,1,2,3)';
            RETURN Tmp;
        END IF;
        RETURN REVERSE(Tmp);
        END$$
    DELIMITER ;
    
    -- select Num_char_extract('字12段',0);
  • 相关阅读:
    c#中使用多线程访问winform中控件的若干问题(转)
    Winform 分页控件(转)
    C#争论:什么时候应该使用var?
    C#的Contains() 值还是引用
    DataTemplate
    DX11_基于GPU_ComputeShader的3D精确拾取
    串行的BitonicSort双调排序
    Directx11_使用Effect框架包装ComputeShader
    Silverlight自适应布局
    poj3626广搜
  • 原文地址:https://www.cnblogs.com/chenzechao/p/11953020.html
Copyright © 2020-2023  润新知