Mysql提取特定列中每个单词的第一个字母

时间:2011-12-29 20:06:29

标签: mysql acronym

我想在表格中创建一个首字母缩略词列。我想从'name'列中获取每个单词的第一个字母,将其大写,然后将所有单词连接到'首字母缩略词'列。

抓住第一个字母的简单方法是什么?

6 个答案:

答案 0 :(得分:6)

这是一个“改进的”功能,允许通过正则表达式过滤所需的字符。

  • function initials执行实际工作,您必须指定正则表达式
  • function acronym仅保留字母数字字符

(如有必要,请在输出中使用upperlowerucase个功能)

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 ;

例1:

select acronym('Come Again? That Cant Help!');

输出:

  

CATCH

例2:

select initials('Come Again? That Cant Help!', '[aeiou]');

输出:

  

oeAaaae

答案 1 :(得分:1)

此类字符串操作不是SQL的设计目的,除非您要为其编写存储过程或UDF。

SQL并不适合这种字符串操作。您 可能 以某种方式 我在Google上进行了长时间的搜索以找到这样的查询语句,但我做不到。只需使用以下功能即可实现您的目标。

drop function if exists initials;
delimiter ||
create function initials(str text) returns text
begin
    declare result text default '';
    declare i int default 1;

    if(str is null) then
        return null;
    end if;

    set result = upper(substr(str, 1, 1));

    while(i <= length(str)) do
        if (substring(str, i, 1) = ' ')
        then
            set result = concat(result, upper(substr(str, i+1, 1)));
        end if;
       set i = i + 1;
    end while;

    return ucase(result);
end;
delimiter ;

答案 2 :(得分:0)

这应该将所有第一个字母放入结果集中:

SELECT UPPER(SUBSTR(name, 0, 1)) FROM the_table

我认为将它们连接成一个单一的首字母缩略词需要某种程序。我认为不能在声明中完成。

答案 3 :(得分:0)

您的意思是LEFTUPPER吗?

答案 4 :(得分:0)

I know this is a little late to the game, but I wanted to offer up a non-function way of doing this for those of you creating a view or a .sql file for periodic use:

SELECT
    @spaces:= length(fi.FacilityName) - length(replace(fi.FacilityName,' ','')) as spaces,
    concat(left(fi.FacilityName,1),
        if(@spaces > 0, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName)+1,1),''),
        if(@spaces > 1, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName, @pos)+1,1),''),
        if(@spaces > 2, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName,@pos)+1,1),''),
        if(@spaces > 3, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName,@pos)+1,1),''),
        if(@spaces > 4, substring(fi.FacilityName,@pos:=locate(' ',fi.FacilityName,@pos)+1,1),'')) as initials
from facilityInfo fi

It's two steps, and you have to include a conditional substring() line for every word you anticipate being in the string, but that is just a copy, paste, and increment of the comparison value for @spaces. My requirements for doing this may be a little looser than some, however. Regardless, it works and causes no noticeable speed issues.

答案 5 :(得分:0)

SELECT REGEXP_REPLACE( ' Bart Van Eynde', ' (.)[^ ]+', '\\1' ); -- 'BVE'
  • 在开始之前在字符串前添加一个空格
  • ' (.)[^ ]+' = 开始寻找空格 + 一些东西(并保存它)+ 如果它不是空格,则忽略其余部分
  • '\\1' 只写回 'something'

在查询中:

SELECT UPPER( REGEXP_REPLACE( CONCAT(' ', col1), ' (.)[^ ]+', '\\1' ) ) from table1;