从 SQL Server 2008 中的字符串中删除所有空格的最佳方法是什么?
LTRIM(RTRIM(' a b '))将删除字符串左右两侧的所有空格,但我还需要删除中间的空格。
LTRIM(RTRIM(' a b '))
只需更换它;
SELECT REPLACE(fld_or_variable, ' ', '')
编辑: 只是为了澄清;它是一个全局替换,不需要trim()或担心多个空格char或varchar:
trim()
char
varchar
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"