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

发布于 2024-12-23 06:59:27 字数 96 浏览 3 评论 0 原文

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

有什么简单的方法来获取第一个字母吗?

I want to create an acronym column in a table. I want to be grab the first letter of each word from a 'name' column, capitalize it, then concatenate all into an 'acronym' column.

Any easy way to grab first letters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

剑心龙吟 2024-12-30 06:59:27

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

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

(使用 upper,如有必要,请在输出上使用 lowerucase 函数)

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!');

输出:

捕捉

示例 2:

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

输出:

oeAaaae

Here is an "improved" function, allowing to filter only wanted characters thanks to a regular expression.

  • function initials does the actual job, you have to specify the regular expression
  • function acronym does the job keeping Alpha-numeric characters only

(Use upper, lower or ucase functions on the output if necessary)
.

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 ;

Example1:

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

Outputs:

CATCH

Example2:

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

Outputs:

oeAaaae

街角卖回忆 2024-12-30 06:59:27

这种字符串操作不是 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 ;

String manipulation of this kind is not what SQL is designed for, unless you want to write a stored procedure or UDF for it.

SQL isn't really suited for string manipulation of this sort. You may do it somehow but why would you when better tools are available elsewhere? I went on a long search on Google to find such a query statement but I couldn't. Just use the following function to achieve what you want.

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 ;
太阳哥哥 2024-12-30 06:59:27

我知道这有点晚了,但我想为那些创建视图或 .sql 文件以供定期使用的人提供一种非函数方式来执行此操作:

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

这是两个步骤,并且您必须包括条件 substring() 行对应您预期出现在字符串中的每个单词,但这只是 @spaces 比较值的复制、粘贴和增量。然而,我这样做的要求可能比某些人宽松一些。无论如何,它可以工作并且不会导致明显的速度问题。

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.

不一样的天空 2024-12-30 06:59:27

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

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

我认为,将它们全部连接成一个首字母缩略词将需要某种过程。我认为这不能通过声明来完成。

This should get all the first letters into a result set:

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

Concatenating them all into a single acronym would, I think, require a procedure of some kind. I don't think it can be done in a statement.

盗琴音 2024-12-30 06:59:27

您的意思是 LEFT上部

Do you mean LEFT and UPPER?

時窥 2024-12-30 06:59:27
SELECT REGEXP_REPLACE( ' Bart Van Eynde', ' (.)[^ ]+', '\\1' ); -- 'BVE'
  • 在开始之前在字符串
  • ' (.)[^ ]+' 之前添加一个空格 ' (.)[^ ]+' = 开始查找空格 + 某些内容(并保存)+ 如果不是空格则忽略其余部分
  • '\\1' 只写 'something'返回

查询:

SELECT UPPER( REGEXP_REPLACE( CONCAT(' ', col1), ' (.)[^ ]+', '\\1' ) ) from table1;
SELECT REGEXP_REPLACE( ' Bart Van Eynde', ' (.)[^ ]+', '\\1' ); -- 'BVE'
  • Before starting add a space before your string
  • ' (.)[^ ]+' = Start looking for space + something (and save it) + ignore the rest if it is not a space
  • '\\1' only write the 'something' back

in a query:

SELECT UPPER( REGEXP_REPLACE( CONCAT(' ', col1), ' (.)[^ ]+', '\\1' ) ) from table1;
山人契 2024-12-30 06:59:27

这适用于 3 个字母的术语:

Select concat(left(Term,1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table

这是 4 个单词的术语:

Select concat(left(Term,1),left(substring_index(Term,' ',-3),1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table

好的,好的,这里是 5 个单词;)

Select concat(left(Term,1),left(substring_index(Term,' ',-4),1),left(substring_index(Term,' ',-3),1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table

This works for 3-letter terms:

Select concat(left(Term,1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table

Here is the one for 4 word terms:

Select concat(left(Term,1),left(substring_index(Term,' ',-3),1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table

Ok ok here is 5 words ;)

Select concat(left(Term,1),left(substring_index(Term,' ',-4),1),left(substring_index(Term,' ',-3),1),left(substring_index(Term,' ',-2),1),left(substring_index(Term,' ',-1),1)) from Table
故人的歌 2024-12-30 06:59:27

这是一个正则表达式解决方案(最初提出这个问题时可能不可能):

SELECT REGEXP_REPLACE(
    REGEXP_REPLACE( 'My Big Fat String', '\\b(\\w)[^\\s]+', '\\1' ), 
    '\\s', 
    ''
); // MBFS
  • \\b matches a word border
  • (\\w) captures the first character
  • [^\\s]+ 消耗单词 \\1 中其余的非空白字符,
  • 仅保留每个单词中捕获的字符

然后外部 REGEXP_REPLACE 从结果中删除空格。

Here is a REGEX solution (likely not possible when this question was originally asked):

SELECT REGEXP_REPLACE(
    REGEXP_REPLACE( 'My Big Fat String', '\\b(\\w)[^\\s]+', '\\1' ), 
    '\\s', 
    ''
); // MBFS
  • \\b matches a word boundary
  • (\\w) captures the first character
  • [^\\s]+ consumes the rest of the non-whitespace characters in the word
  • \\1 keeps only the captured character from each word

Then the outer REGEXP_REPLACE removes the whitespace from the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文