sql - 删除所有空格并将 SQL 中的多行合并为单行

标签 sql sql-server sql-server-2014

在 SQL Server 2014 中删除字符串中所有空格的最佳方法是什么?

我的字符串是:

Maximize your productivity for building engaging,

 beautiful web mapping applications

尝试删除字符串之间的 Enter 和 Tab 空格以及单词之间的 1 个空格。结果应该是这样的:

Maximize your productivity for building engaging, beautiful web mapping applications

最佳答案

您可以使用replace() ,但这有点棘手:

select replace(replace(replace(replace(replace(col, ' ', '<>'
                                              ), '
', '<>'
                                      ), ' ', '<>'  -- tab goes here
                              ), '><', ''
                       ), '<>', ' '
               )
from t;

这个想法是将空格替换为 <> ,然后全部>< s 被删除,只留下一个。例如:

a    b c     -- original data with three spaces and one space
a<><><>b<>c  -- after replacing spaces with <>
a<>b<>c      -- after removing ><
a b c        -- after replacing <> with a space

关于sql - 删除所有空格并将 SQL 中的多行合并为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46225232/

相关文章:

c# - Entity Framework 查询无法使用 asp.net

c# - Entity Framework LINQ to Entities 加入查询超时

sql - 根据刺字 MS SQL Server 选择

sql-server - SQL Server 2014 的 NHibernate 驱动程序和 Sql 方言

sql-server - 在 SQL Server 2014 中将 XML 数据转换为关系格式时遇到问题

mysql - 我应该添加额外的日期列吗?

sql - 在 .sql 文件中查找和替换文本

MySQL - 如何找到开头最相似的单词

sql - 在 SAS (WPS) 中使用 Proc SQL 时出现“无效列名”错误

SQL 一次合并 2 行