修剪SQL Server中字符串的空格,CR,LF

时间:2017-06-30 14:23:15

标签: sql-server

我几乎尝试了一切从sql字符串中删除一些不必要的字符,但它不起作用:

declare @string as char(20)='  my '
select @string as OriginalString
select len(Replace((@string),CHAR(10),''))as StrLen1;
SELECT len(REPLACE(REPLACE(@string, CHAR(13), ' '), CHAR(10), '')) as StrLen2

Output Screen

需要一种方法来完成这项工作。

4 个答案:

答案 0 :(得分:1)

在你似乎提到的三种类型的空白中,空格,换行符和回车符,你的@string只有空格。但你永远不会删除空间。如果要删除所有这三种类型的空格,可以尝试以下操作:

SELECT
    LEN(REPLACE(REPLACE(REPLACE(@string, CHAR(13), ''), CHAR(10), ''), ' ', '')) AS StrLen2

上述查询的输出为2,因为删除空格后我们留下字符串'my'

Demo

答案 1 :(得分:0)

如果你想修剪所有空格,试试这个:

[g[g.Mask != 0] for k, g in df.groupby((df.Mask == 0).cumsum()) if len(g) > 1]

#[   A  B  Mask
# 1  2  3     1
# 2  4  5     1,
#    A  B  Mask
# 4  8  9     1
# 5  5  6     1]

http://m.theseolounge.co.uk/index.php/jadever-jad-jce-counting-scale.html

答案 2 :(得分:0)

如果你只是想摆脱空间:

declare @string as char(20)='  my '
select LTRIM(RTRIM(@string))

答案 3 :(得分:0)

declare @newline char(2)  
set @newline= char(13)+char(10)   --  CR + LF

update **Your Table Here** 

-- ItemCode is column Name,Find and replace values CRLF = ''
Set ItemCode=REPLACE(ItemCode,@newline,'')   
where RIGHT(ItemCode,2) = @newline   -- Maybe no need