• <tfoot id='YqcfC'></tfoot>

    <i id='YqcfC'><tr id='YqcfC'><dt id='YqcfC'><q id='YqcfC'><span id='YqcfC'><b id='YqcfC'><form id='YqcfC'><ins id='YqcfC'></ins><ul id='YqcfC'></ul><sub id='YqcfC'></sub></form><legend id='YqcfC'></legend><bdo id='YqcfC'><pre id='YqcfC'><center id='YqcfC'></center></pre></bdo></b><th id='YqcfC'></th></span></q></dt></tr></i><div id='YqcfC'><tfoot id='YqcfC'></tfoot><dl id='YqcfC'><fieldset id='YqcfC'></fieldset></dl></div>
      <bdo id='YqcfC'></bdo><ul id='YqcfC'></ul>

      <small id='YqcfC'></small><noframes id='YqcfC'>

        <legend id='YqcfC'><style id='YqcfC'><dir id='YqcfC'><q id='YqcfC'></q></dir></style></legend>

        如何删除 MySQL 字段中的前导和尾随空格?

        How to remove leading and trailing whitespace in a MySQL field?(如何删除 MySQL 字段中的前导和尾随空格?)
          <i id='m8UGO'><tr id='m8UGO'><dt id='m8UGO'><q id='m8UGO'><span id='m8UGO'><b id='m8UGO'><form id='m8UGO'><ins id='m8UGO'></ins><ul id='m8UGO'></ul><sub id='m8UGO'></sub></form><legend id='m8UGO'></legend><bdo id='m8UGO'><pre id='m8UGO'><center id='m8UGO'></center></pre></bdo></b><th id='m8UGO'></th></span></q></dt></tr></i><div id='m8UGO'><tfoot id='m8UGO'></tfoot><dl id='m8UGO'><fieldset id='m8UGO'></fieldset></dl></div>

        • <tfoot id='m8UGO'></tfoot>

          <small id='m8UGO'></small><noframes id='m8UGO'>

          <legend id='m8UGO'><style id='m8UGO'><dir id='m8UGO'><q id='m8UGO'></q></dir></style></legend>
            <tbody id='m8UGO'></tbody>

              <bdo id='m8UGO'></bdo><ul id='m8UGO'></ul>
                  本文介绍了如何删除 MySQL 字段中的前导和尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个包含两个字段(国家和 ISO 代码)的表格:

                  I have a table with two fields (countries and ISO codes):

                  Table1
                  
                     field1 - e.g. 'Afghanistan' (without quotes)
                     field2 - e.g. 'AF'(without quotes)
                  

                  在某些行中,第二个字段的开头和/或结尾有空格,这会影响查询.

                  In some rows the second field has whitespace at the start and/or end, which is affecting queries.

                  Table1
                  
                     field1 - e.g. 'Afghanistan' (without quotes) 
                     field2 - e.g. ' AF' (without quotes but with that space in front)
                  

                  有没有办法(在 SQL 中)遍历表并查找/替换 field2 中的空格?

                  Is there a way (in SQL) to go through the table and find/replace the whitespace in field2?

                  推荐答案

                  您正在寻找 TRIM.

                  UPDATE FOO set FIELD2 = TRIM(FIELD2);
                  


                  似乎值得一提的是,TRIM 可以支持多种类型的空格,但一次只能支持一种,并且默认情况下会使用一个空格.但是,您可以嵌套 TRIMs.

                   TRIM(BOTH ' ' FROM TRIM(BOTH '\n' FROM column))
                  

                  如果你真的想在一次调用中去掉所有的空格,你最好使用 REGEXP_REPLACE[[:space:]] 符号.下面是一个例子:

                  If you really want to get rid of all the whitespace in one call, you're better off using REGEXP_REPLACE along with the [[:space:]] notation. Here is an example:

                  SELECT 
                      -- using concat to show that the whitespace is actually removed.
                      CONCAT(
                           '+', 
                           REGEXP_REPLACE(
                               '    ha ppy    ', 
                               -- This regexp matches 1 or more spaces at the beginning with ^[[:space:]]+
                               -- And 1 or more spaces at the end with [[:space:]]+$
                               -- By grouping them with `()` and splitting them with the `|`
                               -- we match all of the expected values.
                               '(^[[:space:]]+|[[:space:]]+$)', 
                  
                               -- Replace the above with nothing
                               ''
                           ), 
                           '+') 
                      as my_example;
                  -- outputs +ha ppy+
                  

                  这篇关于如何删除 MySQL 字段中的前导和尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                      <tbody id='xusje'></tbody>
                    <legend id='xusje'><style id='xusje'><dir id='xusje'><q id='xusje'></q></dir></style></legend>

                      <bdo id='xusje'></bdo><ul id='xusje'></ul>

                      1. <i id='xusje'><tr id='xusje'><dt id='xusje'><q id='xusje'><span id='xusje'><b id='xusje'><form id='xusje'><ins id='xusje'></ins><ul id='xusje'></ul><sub id='xusje'></sub></form><legend id='xusje'></legend><bdo id='xusje'><pre id='xusje'><center id='xusje'></center></pre></bdo></b><th id='xusje'></th></span></q></dt></tr></i><div id='xusje'><tfoot id='xusje'></tfoot><dl id='xusje'><fieldset id='xusje'></fieldset></dl></div>
                        <tfoot id='xusje'></tfoot>

                          <small id='xusje'></small><noframes id='xusje'>