显而易见的方法是使用字符串函数来替换或删除换行符。
REPLACE(@string, CHAR(13), '')
就是不行...
实际上,在SQL命令或脚本字符串中,换行符可以是CR、LF或CR+LF中的任何一种。要获取它们所有的话,需要像这样:
SELECT REPLACE(REPLACE(@str, CHAR(13), ''), CHAR(10), '')
REPLACE(@string, CHAR(13) + CHAR(10), '')
nvarchar
/nchar
字符串时,请使用 NCHAR(13) + NCHAR(10)
,以避免在 n
和非 n
字符串类型之间进行可能昂贵的隐式转换。 - Dai我可能晚了一年才加入这个派对,但我每天都在处理查询和MS-SQL,并且厌倦了内置函数LTRIM()和RTRIM()(总是需要一起调用),以及无法捕捉末尾带有换行符的“脏”数据,因此我决定实现一个更好的TRIM函数。 我欢迎同行们的反馈!
免责声明:这实际上会删除(替换为单个空格)扩展形式的空白(制表符、换行符、回车符等),因此它已从我的原始答案中更名为“CleanAndTrim”。这里的想法是你的字符串不需要这些额外的特殊空格字符,因此如果它们不出现在头/尾部,它们应该被替换为普通空格。 如果您故意存储了这些字符在您的字符串中(比如,您将要运行此函数的数据列),请不要这样做! 改进此函数或编写自己的函数,仅从字符串的端点而不是“正文”中删除这些字符。
好的,现在免责声明已经更新,下面是代码:
-- =============================================
-- Description: TRIMs a string 'for real' - removes standard whitespace from ends,
-- and replaces ASCII-char's 9-13, which are tab, line-feed, vert tab,
-- form-feed, & carriage-return (respectively), with a whitespace
-- (and then trims that off if it's still at the beginning or end, of course).
-- =============================================
CREATE FUNCTION [fn_CleanAndTrim] (
@Str nvarchar(max)
)
RETURNS nvarchar(max) AS
BEGIN
DECLARE @Result nvarchar(max)
SET @Result = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
LTRIM(RTRIM(@Str)), CHAR(9), ' '), CHAR(10), ' '), CHAR(11), ' '), CHAR(12), ' '), CHAR(13), ' ')))
RETURN @Result
END
干杯!
另一个免责声明: 你的典型Windows换行是CR+LF,所以如果你的字符串包含它们,你会用“双倍”空格来替换它们。
更新,2016年: 一个新版本,它为你提供了替换那些特殊空格字符为其他自定义字符的选项!这还包括注释和解决Windows CR+LF配对的解决方法,即将该特定字符对替换为单个替代。
IF OBJECT_ID('dbo.fn_CleanAndTrim') IS NULL
EXEC ('CREATE FUNCTION dbo.fn_CleanAndTrim () RETURNS INT AS BEGIN RETURN 0 END')
GO
-- =============================================
-- Author: Nate Johnson
-- Source: http://stackoverflow.com/posts/24068265
-- Description: TRIMs a string 'for real' - removes standard whitespace from ends,
-- and replaces ASCII-char's 9-13, which are tab, line-feed, vert tab, form-feed,
-- & carriage-return (respectively), with a whitespace or specified character(s).
-- Option "@PurgeReplaceCharsAtEnds" determines whether or not to remove extra head/tail
-- replacement-chars from the string after doing the initial replacements.
-- This is only truly useful if you're replacing the special-chars with something
-- **OTHER** than a space, because plain LTRIM/RTRIM will have already removed those.
-- =============================================
ALTER FUNCTION dbo.[fn_CleanAndTrim] (
@Str NVARCHAR(MAX)
, @ReplaceTabWith NVARCHAR(5) = ' '
, @ReplaceNewlineWith NVARCHAR(5) = ' '
, @PurgeReplaceCharsAtEnds BIT = 1
)
RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE @Result NVARCHAR(MAX)
--The main work (trim & initial replacements)
SET @Result = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
LTRIM(RTRIM(@Str)) --Basic trim
, NCHAR(9), @ReplaceTabWith), NCHAR(11), @ReplaceTabWith) --Replace tab & vertical-tab
, (NCHAR(13) + NCHAR(10)), @ReplaceNewlineWith) --Replace "Windows" linebreak (CR+LF)
, NCHAR(10), @ReplaceNewlineWith), NCHAR(12), @ReplaceNewlineWith), NCHAR(13), @ReplaceNewlineWith))) --Replace other newlines
--If asked to trim replacement-char's from the ends & they're not both whitespaces
IF (@PurgeReplaceCharsAtEnds = 1 AND NOT (@ReplaceTabWith = N' ' AND @ReplaceNewlineWith = N' '))
BEGIN
--Purge from head of string (beginning)
WHILE (LEFT(@Result, DATALENGTH(@ReplaceTabWith)/2) = @ReplaceTabWith)
SET @Result = SUBSTRING(@Result, DATALENGTH(@ReplaceTabWith)/2 + 1, DATALENGTH(@Result)/2)
WHILE (LEFT(@Result, DATALENGTH(@ReplaceNewlineWith)/2) = @ReplaceNewlineWith)
SET @Result = SUBSTRING(@Result, DATALENGTH(@ReplaceNewlineWith)/2 + 1, DATALENGTH(@Result)/2)
--Purge from tail of string (end)
WHILE (RIGHT(@Result, DATALENGTH(@ReplaceTabWith)/2) = @ReplaceTabWith)
SET @Result = SUBSTRING(@Result, 1, DATALENGTH(@Result)/2 - DATALENGTH(@ReplaceTabWith)/2)
WHILE (RIGHT(@Result, DATALENGTH(@ReplaceNewlineWith)/2) = @ReplaceNewlineWith)
SET @Result = SUBSTRING(@Result, 1, DATALENGTH(@Result)/2 - DATALENGTH(@ReplaceNewlineWith)/2)
END
RETURN @Result
END
GO
T-SQL中的换行符由CHAR(13)和CHAR(10)(回车+换行)表示。因此,您可以创建一个REPLACE语句,将要替换的文本与换行符替换。
REPLACE(MyField, CHAR(13) + CHAR(10), 'something else')
为了做到大多数人想要的,创建一个不是实际换行符的占位符。然后你可以将以下方法结合起来使用:
REPLACE(REPLACE(REPLACE(MyField, CHAR(13) + CHAR(10), 'something else'), CHAR(13), 'something else'), CHAR(10), 'something else')
这样只会替换一次。不同于:
REPLACE(REPLACE(MyField, CHAR(13), ''), CHAR(10), '')
如果你只是想去掉CRLF字符,那么它的效果非常好,但如果你需要一个占位符,比如
<br/>
如果想要更准确一些,那么第一种方法会比较合适。
Select Trim(char(10) + char(13) from @str)
我用它来修剪文件名中的特殊字符。
Select Trim(char(10) + char(13) + ' *<>' from @fileName)
在这种情况下,您需要将文本强制转换为nvarchar,然后再进行替换。Msg 8116, Level 16, State 1, Line 2 Argument data type text is invalid for argument 1 of replace function.
SELECT REPLACE(REPLACE(cast(@str as nvarchar(max)), CHAR(13), ''), CHAR(10), '')
REPLACE(myString, CHAR(13) + CHAR(10), ' ')
无法工作。在这种情况下,请使用以下代码片段:
REPLACE(REPLACE(myString, CHAR(13),''), CHAR(10), ' ')
如果你只想删除字符末尾的内容,可以尝试以下方法:
WHILE EXISTS
(SELECT * FROM @ReportSet WHERE
ASCII(right(addr_3,1)) = 10
OR ASCII(right(addr_3,1)) = 13
OR ASCII(right(addr_3,1)) = 32)
BEGIN
UPDATE @ReportSet
SET addr_3 = LEFT(addr_3,LEN(addr_3)-1)
WHERE
ASCII(right(addr_3,1)) = 10
OR ASCII(right(addr_3,1)) = 13
OR ASCII(right(addr_3,1)) = 32
END
这解决了我在地址方面遇到的问题,其中一个过程创建了一个带有固定行数的字段,即使这些行是空的。为了节省我的SSRS报告的空间,我把它们缩小了。
REPLACE(REPLACE(REPLACE(REPLACE(ErrorMessage, CHAR(13), ''), CHAR(10), ''),',',''),'''','''''')
可能有更好的方法,但它完成了工作。
.columns[0]
代替。 - RBarryYoung