我想在表格中创建一个首字母缩略词列.我想从'name'列中获取每个单词的第一个字母,将其大写,然后将所有单词连接到'首字母缩略词'列.
抓住第一个字母的任何简单方法?
这是一个"改进的"功能,允许通过正则表达式过滤所需的字符.
initials执行实际工作,您必须指定正则表达式acronym只保留字母数字字符(使用upper,lower或ucase在输出功能如果必要的话).
delimiter $$
drop function if exists `initials`$$
CREATE FUNCTION `initials`(str text, expr text) RETURNS text CHARSET utf8
begin
declare result text default '';
declare buffer text default '';
declare i int default 1;
if(str is null) then
return null;
end if;
set buffer = trim(str);
while i <= length(buffer) do
if substr(buffer, i, 1) regexp expr then
set result = concat( result, substr( buffer, i, 1 ));
set i = i + 1;
while i <= length( buffer ) and substr(buffer, i, 1) regexp expr do
set i = i + 1;
end while;
while i <= length( buffer ) and substr(buffer, i, 1) not regexp expr do
set i = i + 1;
end while;
else
set i = i + 1;
end if;
end while;
return result;
end$$
drop function if exists `acronym`$$
CREATE FUNCTION `acronym`(str text) RETURNS text CHARSET utf8
begin
declare result text default '';
set result = initials( str, '[[:alnum:]]' );
return result;
end$$
delimiter ;
Run Code Online (Sandbox Code Playgroud)
例1:
select acronym('Come Again? That Cant Help!');
Run Code Online (Sandbox Code Playgroud)
输出:
抓住
例2:
select initials('Come Again? That Cant Help!', '[aeiou]');
Run Code Online (Sandbox Code Playgroud)
输出:
oeAaaae