MySQL:如何从字符串中删除双精度或更多空格? [英] MySQL : how to remove double or more spaces from a string?

查看:93
本文介绍了MySQL:如何从字符串中删除双精度或更多空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到MySQL的这个问题,所以在这里:

I couldn't find this question for MySQL so here it is:

我需要将字符串中的所有双精度或多精度空格修剪为1个单空格.

I need to trim all double or more spaces in a string to 1 single space.

例如: 快速"布朗       " Fox" 应该 : 快棕色狐狸"

For example: "The   Quick  Brown    Fox" should be : "The Quick Brown Fox"

函数REPLACE(str, ",")仅删除双精度空格,但当有更多空格时则保留多精度空格.

The function REPLACE(str, "  ", " ") only removes double spaces, but leaves multiples spaces when there are more...

推荐答案

最短,最令人惊讶的是最快的解决方案:

The shortest and, surprisingly, the fastest solution:

CREATE FUNCTION clean_spaces(str VARCHAR(255)) RETURNS VARCHAR(255)
BEGIN
    while instr(str, '  ') > 0 do
        set str := replace(str, '  ', ' ');
    end while;
    return trim(str);
END

这篇关于MySQL:如何从字符串中删除双精度或更多空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆