在Mysql中创建了一个存储过程,用来分隔字符串,如下:
- DROP TABLE IF EXISTS `GetPopWord`;
- CREATE DEFINER=`scada`@`%` PROCEDURE `GetPopWord`(inout SourceString nvarchar(4000) , inout Firstword nvarchar(4000),delimiter nvarchar(10))
- begin
- DECLARE Oldword NVARCHAR(4000);
- DECLARE LengthSource INT;
- DECLARE cur_position INT;
- set Oldword = SourceString;
- if Oldword is not null then
- SET cur_position = INSTR(Oldword,delimiter);
- SET LengthSource = CHAR_LENGTH(Oldword);
- if cur_position = 0 then
- set Firstword = Oldword;
- set SourceString = '';
- else
- set Firstword = LEFT(Oldword,cur_position -1 );
- set SourceString=SUBSTRING(Oldword,cur_position+1,LengthSource- cur_position);
- end if;
- else
- set SourceString = null;
- set Firstword = null;
- end if;
- end;
在Mysql Front的界面的查询页面中,安如下语法调用:
set @b = 'sss,bbb,cc';
set @c = '';
call GetPopWord(@b,@c,',');
select @c as `第一个字符` ,@b as `剩下的` ;
执行的结果是:
第一个字符 剩下的
sss bbb,cc