SQL删除单个变量的换行符

时间:2019-12-06 20:12:03

标签: sql sql-server

我有一列返回一行HTML文本。当我将结果复制到Excel时,该列将使用HTML代码格式化电子表格。

我正在尝试删除换行符,以便将结果保留在一行中。

我设法使用下面的代码删除了
,但是现在输出将所有结果压缩为一行,而不仅仅是一行。

例如

所需结果

<br>Column1 &nbsp;&nbsp;&nbsp;Column2&nbsp;&nbsp;&nbsp;Column3
<br>A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;html&gt;&lt;head&gt;&lt;p&gt;I have a question&lt;/p&gt;&lt;p&gt;Here is the answer to my question&lt;/p&gt;&lt;/html&gt;
<br>B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;html&gt;&lt;head&gt;&lt;p&gt;Hello world&lt;/p&gt;&lt;p&gt;This is a response&lt;/p&gt;&lt;/html&gt;
<br>C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;html&gt;&lt;head&gt;&lt;p&gt;Do you have another question&lt;/p&gt;&lt;p&gt;I have another question&lt;/p&gt;&lt;/html&gt;

当前结果

A 1 &lt;html&gt;&lt;head&gt;&lt;p&gt;I have a question&lt;/p&gt;&lt;p&gt;Here is the answer to my question&lt;/p&gt;&lt;/html&gt;B 2 &lt;html&gt;&lt;head&gt;&lt;p&gt;Hello world&lt;/p&gt;&lt;p&gt;This is a response&lt;/p&gt;&lt;/html&gt;C 3 &lt;html&gt;&lt;head&gt;&lt;p&gt;Do you have another question&lt;/p&gt;&lt;p&gt;I have another question&lt;/p&gt;&lt;/html&gt;

到目前为止已尝试的代码
(Column3中包含HTML)

SELECT
Column1
,Column2
,REPLACE(CAST(Column3 AS CHAR), '<br%','')
FROM
Table1
WHERE
CAST(Column3 AS VARCHAR(MAX) LIKE '%m%'

1 个答案:

答案 0 :(得分:1)

您可以使用REPLACE删除回车符和换行符:

REPLACE(REPLACE(YourColumn,CHAR(13),''),CHAR(10),'')