4 回答

TA貢獻1856條經(jīng)驗 獲得超5個贊
更整潔:
select string = replace(replace(replace(' select single spaces',' ','<>'),'><',''),'<>',' ')
輸出:
選擇單個空格

TA貢獻1995條經(jīng)驗 獲得超2個贊
這將工作:
declare @test varchar(100)
set @test = 'this is a test'
while charindex(' ',@test ) > 0
begin
set @test = replace(@test, ' ', ' ')
end
select @test

TA貢獻1772條經(jīng)驗 獲得超5個贊
如果您知道一行中最多只能有一定數(shù)量的空格,則可以嵌套替換:
replace(replace(replace(replace(myText,' ',' '),' ',' '),' ',' '),' ',' ')
4個替換項最多可固定16個連續(xù)空格(16個,然后8個,然后4個,然后2個,然后1個)
如果可能要長得多,那么您必須執(zhí)行類似內(nèi)聯(lián)函數(shù)的操作:
CREATE FUNCTION strip_spaces(@str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
WHILE CHARINDEX(' ', @str) > 0
SET @str = REPLACE(@str, ' ', ' ')
RETURN @str
END
然后就做
SELECT dbo.strip_spaces(myText) FROM myTable

TA貢獻1829條經(jīng)驗 獲得超4個贊
update mytable
set myfield = replace (myfield, ' ', ' ')
where charindex(' ', myfield) > 0
替換將在所有雙精度空格上起作用,而無需進行多次替換。這是基于集合的解決方案。
- 4 回答
- 0 關(guān)注
- 730 瀏覽
添加回答
舉報