简体   繁体   English

MySQL 版本 8.0.17 - 仅替换字符串中的第一个空格

[英]MySQL version 8.0.17 - Replacing only first space in string

In the table stored an a database MySql version 8.0.17 I have this string在表中存储了一个数据库MySql version 8.0.17我有这个字符串

Sheriff Curtis Edward

I need replace in this string only the first space for this return我只需要在这个字符串中替换这个返回的第一个空格

SheriffCurtis Edward

This will make future surname entries work even if the given surname is a double surname...即使给定的姓氏是双姓,这也将使未来的姓氏条目起作用......

I have tried this sql but the return is wrong...我试过这个 sql 但返回错误...

SELECT SUBSTRING('Sheriff Curtis Edward',
                 INSTR('Sheriff Curtis Edward', ' ') + 1,
                 CHAR_LENGTH('Sheriff Curtis Edward') - INSTR('Sheriff Curtis Edward', ' ')) new_string;
+---------------+
| new_string    |
+---------------+
| Curtis Edward |
+---------------+
1 row in set (0.10 sec)

You could use REGEXP_REPLACE here with a capture group:您可以在此处将REGEXP_REPLACE与捕获组一起使用:

SELECT
    col,
    REGEXP_REPLACE(col, '^(\\S+) ', '$1') AS col_out
FROM yourTable;

Demo 演示

Using your subtring() approach, you could also extract the substring before the space and concatenate it with the substring after the space.使用您的subtring()方法,您还可以在空格之前提取 substring 并将其与空格之后的 substring 连接。

SELECT concat(substring(x.s,
                        1,
                        instr(x.s,
                              ' ')
                        - 1),
              substring(x.s,
                        instr(x.s,
                              ' ')
                        + 1,
                        char_length(x.s)
                        - instr(x.s,
                                ' ')))
       FROM (SELECT 'Sheriff Curtis Edward' s) x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM