一般来说,制表符、换行符、回车是罪魁祸首,但如果遇到无法删除的空白字符,则可以创建如下函数:
CREATE FUNCTION [dbo].[fn_Trim]
(
@str as varchar(1000)
)
RETURNS varchar(1000)
AS
BEGIN
--Replace all whitespace characers with Characer 32 whitespace
--NULL
Set @str = Replace(@str,CHAR(0),CHAR(32));
--Horizontal Tab
Set @str = Replace(@str,CHAR(9),CHAR(32));
--Line Feed
Set @str = Replace(@str,CHAR(10),CHAR(32));
--Vertical Tab
Set @str = Replace(@str,CHAR(11),CHAR(32));
--Form Feed
Set @str = Replace(@str,CHAR(12),CHAR(32));
--Carriage Return
Set @str = Replace(@str,CHAR(13),CHAR(32));
--Column Break
Set @str = Replace(@str,CHAR(14),CHAR(32));
--Non-breaking space
Set @str = Replace(@str,CHAR(160),CHAR(32));
Set @str = LTRIM(RTRIM(@str));
Return @str
END
GO
然后使用它将列更新为:
Update Table_Names
set Name_ID = dbo.fn_Trim(Name_ID)