您好,欢迎访问一九零五行业门户网

mysql字符串查找截取与正则表达式的联合应用

/* 判断字符串里的内容是否是数类型 **************************************************** is_double 输入参数: str: 待分析的字符串 返回: 如果是数类型就返回1,否则返回0 */ delimiter $$ drop function if exists is_double$$ create function is_doubl
/* 判断字符串里的内容是否是数值类型 ****************************************************
    is_double
输入参数:
        str:            待分析的字符串
    返回:                如果是数值类型就返回1,否则返回0
*/
delimiter $$
drop function if exists is_double$$
create function is_double(str varchar(128))
returns int
begin
    declare iresult int default 0;
/*null字符串或者空字符串*/
    if str is null or str = ''then
        return 0;
    end if;
select str regexp '^[[:digit:]]+$|^[[:digit:]]+[\\.][[:digit:]]+$' into iresult;
if iresult = 1 then
        return 1;
    else
        return 0;
    end if;
end $$
delimiter ;
/* 分割字符串 ****************************************************
    sp_report_string_get_val
功能:分割特定格式的字符串:'3.4,3.2,3.0,2.6,1.0',忽略空值或者是不合法的值
    限制:以逗号分割,分割单元以数字组成
    结果:分割之后 字符串变为'3.2,3.0,2.6,1.0'和3.4
输入输出参数:
        prstring:        要分割的字符串
    输出参数:
        rval:            从字符串中取得的数值
        rcode:            返回代码:0:表示成功执行;1:表示没有取到值或者取到的不是数值
*/
drop procedure if exists sp_get_val;
create procedure sp_get_val(
                                                    inout    prstring    varchar(128),
                                                    out        rval        double,
                                                    out        rcode        int unsigned
                                                    )
begin
    declare _pos    int unsigned default 1;        /*','的位置*/
    declare _str    varchar(128) default '';    /*存放取出的字符串,之后将值赋给返回变量*/
set rval = null;
    set rcode = 0;
    /*
        1)先去除空格
    */
    set prstring = replace(prstring, ' ', '');
    if prstring = '' then
        set rcode = 1;
    else
        if right(prstring, 1) != ',' then
            set prstring = concat(prstring, ',');
        end if;
/*
            2)找到第一个','
        */
        while rval is null and prstring != '' do
            set _pos = locate(',', prstring);
if _pos = 0 then
                set rcode = 1;
            else
                set _str = substring(prstring, 1, _pos - 1);
if is_double(_str) then
                    set rval = _str;
                end if;
set prstring = substring(prstring, _pos + 1);
            end if;
        end while;
    end if;
if rval is null then
        set rcode = 1;
    end if;
end;
测试:
create table test_1(a double);
delimiter $$
drop procedure if exists sp_test_3$$
create procedure sp_test_3()
begin
    declare _str    varchar(128) default '1,4,3.5,3.7,3.22a, , a, 34.2, 0, .1, 0., ';
    declare _i        double default 0;
    declare _j        int unsigned default 0;
while _str != '' do
        call sp_get_val(_str, _i, _j);
        if _j = 0 then
            insert into test_1 values(_i);
        end if;
    end while;
end$$
delimiter ;
执行:
call sp_test_3();
select * from test_1;
truncate test_1;
其它类似信息

推荐信息