我需要确保一个给定的字段在字符之间没有多个空格(我不关心所有空格,只关心空格).
所以
'single spaces only'
Run Code Online (Sandbox Code Playgroud)
需要变成
'single spaces only'
Run Code Online (Sandbox Code Playgroud)
以下不起作用
select replace('single spaces only',' ',' ')
Run Code Online (Sandbox Code Playgroud)
因为它会导致
'single spaces only'
Run Code Online (Sandbox Code Playgroud)
我更喜欢坚持使用原生T-SQL而不是基于CLR的解决方案.
思考?
Nei*_*ght 295
更整洁:
select string = replace(replace(replace(' select single spaces',' ','<>'),'><',''),'<>',' ')
Run Code Online (Sandbox Code Playgroud)
输出:
选择单个空格
Jam*_*man 25
这可行:
declare @test varchar(100)
set @test = 'this is a test'
while charindex(' ',@test ) > 0
begin
set @test = replace(@test, ' ', ' ')
end
select @test
Run Code Online (Sandbox Code Playgroud)
Bra*_*adC 16
如果您知道连续的空格不会超过一定数量,则可以嵌套替换:
replace(replace(replace(replace(myText,' ',' '),' ',' '),' ',' '),' ',' ')
Run Code Online (Sandbox Code Playgroud)
4个替换应该最多固定16个空格(16个,然后是8个,然后是4个,然后是2个,然后是1个)
如果它可能显着更长,那么你必须做一些像内联函数:
CREATE FUNCTION strip_spaces(@str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
WHILE CHARINDEX(' ', @str) > 0
SET @str = REPLACE(@str, ' ', ' ')
RETURN @str
END
Run Code Online (Sandbox Code Playgroud)
然后就做
SELECT dbo.strip_spaces(myText) FROM myTable
Run Code Online (Sandbox Code Playgroud)
update mytable
set myfield = replace (myfield, ' ', ' ')
where charindex(' ', myfield) > 0
Run Code Online (Sandbox Code Playgroud)
替换将适用于所有双空格,无需多次替换.这是基于集合的解决方案.
它可以通过函数递归完成:
CREATE FUNCTION dbo.RemSpaceFromStr(@str VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS
BEGIN
RETURN (CASE WHEN CHARINDEX(' ', @str) > 0 THEN
dbo.RemSpaceFromStr(REPLACE(@str, ' ', ' ')) ELSE @str END);
END
Run Code Online (Sandbox Code Playgroud)
然后,例如:
SELECT dbo.RemSpaceFromStr('some string with many spaces') AS NewStr
Run Code Online (Sandbox Code Playgroud)
返回:
NewStr
some string with many spaces
Run Code Online (Sandbox Code Playgroud)
或者基于@agdk26 或@Neil Knight(但更安全)描述的方法的解决方案(但更安全)
两个示例都返回上面的输出:
SELECT REPLACE(REPLACE(REPLACE('some string with many spaces'
, ' ', ' ' + CHAR(7)), CHAR(7) + ' ', ''), ' ' + CHAR(7), ' ') AS NewStr
--but it remove CHAR(7) (Bell) from string if exists...
Run Code Online (Sandbox Code Playgroud)
或者
SELECT REPLACE(REPLACE(REPLACE('some string with many spaces'
, ' ', ' ' + CHAR(7) + CHAR(7)), CHAR(7) + CHAR(7) + ' ', ''), ' ' + CHAR(7) + CHAR(7), ' ') AS NewStr
--but it remove CHAR(7) + CHAR(7) from string
Run Code Online (Sandbox Code Playgroud)
注意:
用于替换空格的字符/字符串不应存在于字符串的开头或结尾并且是独立的。
这有点蛮力,但会起作用
CREATE FUNCTION stripDoubleSpaces(@prmSource varchar(max)) Returns varchar(max)
AS
BEGIN
WHILE (PATINDEX('% %', @prmSource)>0)
BEGIN
SET @prmSource = replace(@prmSource ,' ',' ')
END
RETURN @prmSource
END
GO
-- Unit test --
PRINT dbo.stripDoubleSpaces('single spaces only')
single spaces only
Run Code Online (Sandbox Code Playgroud)
小智 5
这是我创建的一个简单函数,用于清除字符串之前或之后的任何空格以及字符串中的多个空格。它可以优雅地处理单次拉伸中最多大约 108 个空格以及字符串中尽可能多的块。如果需要,您可以通过添加具有更大空间块的附加行来将其增加 8 倍。尽管它在大型应用程序中得到广泛使用,但它似乎执行得很快并且没有造成任何问题。
CREATE FUNCTION [dbo].[fnReplaceMultipleSpaces] (@StrVal AS VARCHAR(4000))
RETURNS VARCHAR(4000)
AS
BEGIN
SET @StrVal = Ltrim(@StrVal)
SET @StrVal = Rtrim(@StrVal)
SET @StrVal = REPLACE(@StrVal, ' ', ' ') -- 16 spaces
SET @StrVal = REPLACE(@StrVal, ' ', ' ') -- 8 spaces
SET @StrVal = REPLACE(@StrVal, ' ', ' ') -- 4 spaces
SET @StrVal = REPLACE(@StrVal, ' ', ' ') -- 2 spaces
SET @StrVal = REPLACE(@StrVal, ' ', ' ') -- 2 spaces (for odd leftovers)
RETURN @StrVal
END
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
100567 次 |
最近记录: |