在SQL Server 2008中删除字符串中的所有空格的最佳方法是什么?
LTRIM(RTRIM(' a b '))将删除字符串右侧和左侧的所有空格,但我还需要删除中间的空格。
在SQL Server 2008中删除字符串中的所有空格的最佳方法是什么?
LTRIM(RTRIM(' a b '))将删除字符串右侧和左侧的所有空格,但我还需要删除中间的空格。
当前回答
如果字符串中有多个空格,则replace可能无法正常工作。为此,应该使用下面的函数。
CREATE FUNCTION RemoveAllSpaces
(
@InputStr varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN
declare @ResultStr varchar(8000)
set @ResultStr = @InputStr
while charindex(' ', @ResultStr) > 0
set @ResultStr = replace(@InputStr, ' ', '')
return @ResultStr
END
例子:
select dbo.RemoveAllSpaces('aa aaa aa aa a')
输出:
aaaaaaaaaa
其他回答
我分享一个解决方案,在我看来,它非常快,但安装起来有点繁琐。它在Microsoft SQL Server 2008 R2 (SP3)上工作得很好:
在数据库所在的服务器上安装Sql Regex程序集。安装步骤如下: https://github.com/DevNambi/sql-server-regex#installation 在数据库中创建函数dbo。TRIM,它基于RegexReplace()标量函数(https://github.com/DevNambi/sql-server-regex#replace):
CREATE FUNCTION [dbo].[TRIM](@text NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) --WITH SCHEMABINDING
BEGIN
-- This function removes:
-- 1. invisible characters,
-- 2. repeated blank spaces and
-- 3. the spaces at the beginning and at the end of the text.
RETURN (CASE
WHEN @text is NULL
THEN NULL
ELSE
dbo.RegexReplace((dbo.RegexReplace(dbo.RegexReplace(@text, N'['+Nchar(0)+N'-'+Nchar(32)+Nchar(8192)+N'-'+Nchar(8202)+Nchar(160)+Nchar(5760)+Nchar(6158)+Nchar(8232)+Nchar(8233)+Nchar(8239)+Nchar(8287)+Nchar(65440)+Nchar(12288) +N']+', N'[}'), N'[\[\}]+', ' ')), N'^\s+|\s+$','')
END);
END
GO
....................
使用方法:
declare @txt NVARCHAR(MAX) = N' Hello,'+Nchar(12288)+N' my '+NCHAR(160)+N'name'+Nchar(0)+N' is John'+NCHAR(11)+N' Doe';
select dbo.TRIM(@txt) as Result -- Result: "Hello, my name is John Doe"
....................
更新:
要完全删除所有空格,您可以创建以下函数:
CREATE FUNCTION [dbo].[TRIM_SPACES](@text NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) --WITH SCHEMABINDING
BEGIN
RETURN (CASE WHEN @text is NULL THEN NULL ELSE
dbo.RegexReplace(@text, N'['+Nchar(0)+N'-'+Nchar(32)+Nchar(8192)+N'-'+Nchar(8202)+Nchar(160)+Nchar(5760)+Nchar(6158)+Nchar(8232)+Nchar(8233)+Nchar(8239)+Nchar(8287)+Nchar(65440)+Nchar(12288) +N']+', N'')
END)
END
GO
....................
使用方法:
declare @txt NVARCHAR(MAX) = N' Hello,'+Nchar(12288)+N' my '+NCHAR(160)+N'name'+Nchar(0)+N' is John'+NCHAR(11)+N' Doe';
select dbo.TRIM_SPACES(@txt) as Result -- Result: "Hello,mynameisJohnDoe"
一个功能版本(udf),删除空格,cr, lf,制表符或可配置。
select Common.ufn_RemoveWhitespace(' 234 asdf wefwef 3 x ', default) as S
结果:234 asdfwefwef3x
alter function Common.RemoveWhitespace
(
@pString nvarchar(max),
@pWhitespaceCharsOpt nvarchar(max) = null -- default: tab, lf, cr, space
)
returns nvarchar(max) as
/*--------------------------------------------------------------------------------------------------
Purpose: Compress whitespace
Example: select Common.ufn_RemoveWhitespace(' 234 asdf wefwef 3 x ', default) as s
-- Result: 234asdfwefwef3x
Modified By Description
---------- ----------- --------------------------------------------------------------------
2018.07.24 crokusek Initial Version
--------------------------------------------------------------------------------------------------*/
begin
declare
@maxLen bigint = 1073741823, -- (2^31 - 1) / 2 (https://stackoverflow.com/a/4270085/538763)
@whitespaceChars nvarchar(30) = coalesce(
@pWhitespaceCharsOpt,
char(9) + char(10) + char(13) + char(32)); -- tab, lf, cr, space
declare
@whitespacePattern nvarchar(30) = '%[' + @whitespaceChars + ']%',
@nonWhitespacePattern nvarchar(30) = '%[^' + @whitespaceChars + ']%',
@previousString nvarchar(max) = '';
while (@pString != @previousString)
begin
set @previousString = @pString;
declare
@whiteIndex int = patindex(@whitespacePattern, @pString);
if (@whiteIndex > 0)
begin
declare
@whitespaceLength int = nullif(patindex(@nonWhitespacePattern, substring(@pString, @whiteIndex, @maxLen)), 0) - 1;
set @pString =
substring(@pString, 1, @whiteIndex - 1) +
iif(@whiteSpaceLength > 0, substring(@pString, @whiteIndex + @whiteSpaceLength, @maxLen), '');
end
end
return @pString;
end
go
如果正规空格没有被LTRM或RTRM删除,请尝试这样使用
LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(Column_data, CHAR(9), ''), CHAR(10), ''), CHAR(13), '')))
replace(replace(column_Name,CHAR(13),''),CHAR(10),'')
如果你需要在所有列中修剪空格,你可以使用这个脚本来动态地做它:
--Just change table name
declare @MyTable varchar(100)
set @MyTable = 'MyTable'
--temp table to get column names and a row id
select column_name, ROW_NUMBER() OVER(ORDER BY column_name) as id into #tempcols from INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE IN ('varchar', 'nvarchar') and TABLE_NAME = @MyTable
declare @tri int
select @tri = count(*) from #tempcols
declare @i int
select @i = 0
declare @trimmer nvarchar(max)
declare @comma varchar(1)
set @comma = ', '
--Build Update query
select @trimmer = 'UPDATE [dbo].[' + @MyTable + '] SET '
WHILE @i <= @tri
BEGIN
IF (@i = @tri)
BEGIN
set @comma = ''
END
SELECT @trimmer = @trimmer + CHAR(10)+ '[' + COLUMN_NAME + '] = LTRIM(RTRIM([' + COLUMN_NAME + ']))'+@comma
FROM #tempcols
where id = @i
select @i = @i+1
END
--execute the entire query
EXEC sp_executesql @trimmer
drop table #tempcols