create table #t (
c char(8),
v varchar(8))
insert #t (c, v) values
('a a' , 'a a' ),
('a a ' , 'a a ' ),
(' a a' , ' a a' ),
(' a a ', ' a a ')
select
'"' + c + '"' [IN], '"' + replace(c, ' ', '') + '"' [OUT]
from #t
union all select
'"' + v + '"', '"' + replace(v, ' ', '') + '"'
from #t
结果
IN OUT
===================
"a a " "aa"
"a a " "aa"
" a a " "aa"
" a a " "aa"
"a a" "aa"
"a a " "aa"
" a a" "aa"
" a a " "aa"
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
--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
CREATE TABLE tbl_RemoveExtraSpaces
(
Rno INT
,Name VARCHAR(100)
)
GO
INSERT INTO tbl_RemoveExtraSpaces VALUES (1,'I am Anvesh Patel')
INSERT INTO tbl_RemoveExtraSpaces VALUES (2,'Database Research and Development ')
INSERT INTO tbl_RemoveExtraSpaces VALUES (3,'Database Administrator ')
INSERT INTO tbl_RemoveExtraSpaces VALUES (4,'Learning BIGDATA and NOSQL ')
GO
脚本选择字符串没有额外的空格:
SELECT
[Rno]
,[Name] AS StringWithSpace
,LTRIM(RTRIM(REPLACE(REPLACE(REPLACE([Name],CHAR(32),'()'),')(',''),'()',CHAR(32)))) AS StringWithoutSpace
FROM tbl_RemoveExtraSpaces
结果:
Rno StringWithSpace StringWithoutSpace
----------- ----------------------------------------- ---------------------------------------------
1 I am Anvesh Patel I am Anvesh Patel
2 Database Research and Development Database Research and Development
3 Database Administrator Database Administrator
4 Learning BIGDATA and NOSQL Learning BIGDATA and NOSQL
UPDATE table_name SET "column_name"=replace("column_name", ' ', ''); //Remove white space
UPDATE table_name SET "column_name"=replace("column_name", '\n', ''); //Remove newline
UPDATE table_name SET "column_name"=replace("column_name", '\t', ''); //Remove all tab
CREATE FUNCTION dbo.TRIM(@String VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String,CHAR(10),'[]'),CHAR(13),'[]'),char(9),'[]'),CHAR(32),'[]'),'][',''),'[]',CHAR(32))));
END
GO
似乎每个人都一直在引用一个REPLACE函数。甚至多次调用REPLACE函数。但是当你有一个未知数量的空格的动态输出时,它就不起作用了。任何经常处理这个问题的人都知道REPLACE只会删除一个空格,而不是全部。LTRIM和RTRIM似乎有同样的问题。留给微软吧。
下面是一个示例输出,使用WHILE循环删除所有CHAR(32)值(空格)。< / p >
DECLARE @INPUT_VAL VARCHAR(8000)
DECLARE @OUTPUT_VAL VARCHAR(8000)
SET @INPUT_VAL = ' C A '
SET @OUTPUT_VAL = @INPUT_VAL
WHILE CHARINDEX(CHAR(32), @OUTPUT_VAL) > 0 BEGIN
SET @OUTPUT_VAL = REPLACE(@INPUT_VAL, CHAR(32), '')
END
PRINT 'START:' + @INPUT_VAL + ':END'
PRINT 'START:' + @OUTPUT_VAL + ':END'
下面是上面代码的输出:
START: C A :END
START:CA:END
现在更进一步,在UPDATE或SELECT语句中使用它,将其更改为udf。
CREATE FUNCTION udf_RemoveSpaces (@INPUT_VAL VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @OUTPUT_VAL VARCHAR(8000)
SET @OUTPUT_VAL = @INPUT_VAL
-- ITTERATE THROUGH STRING TO LOOK FOR THE ASCII VALUE OF SPACE (CHAR(32)) REPLACE IT WITH BLANK, NOT NULL
WHILE CHARINDEX(CHAR(32), @OUTPUT_VAL) > 0 BEGIN
SET @OUTPUT_VAL = REPLACE(@INPUT_VAL, CHAR(32), '')
END
RETURN @OUTPUT_VAL
END
然后在SELECT或INSERT语句中使用该函数:
UPDATE A
SET STATUS_REASON_CODE = WHATEVER.dbo.udf_RemoveSpaces(STATUS_REASON_CODE)
FROM WHATEVER..ACCT_INFO A
WHERE A.SOMEVALUE = @SOMEVALUE
INSERT INTO SOMETABLE
(STATUS_REASON_CODE)
SELECT WHATEVER.dbo.udf_RemoveSpaces(STATUS_REASON_CODE)
FROM WHATEVER..ACCT_INFO A
WHERE A.SOMEVALUE = @SOMEVALUE
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"