【发布时间】:2018-10-11 20:02:04
【问题描述】:
这个功能很好用,但我想保留空格。任何帮助将不胜感激
create function dbo.RemoveSpecialChars
(@s varchar(256)) returns varchar(256)
with schemabinding
begin
if @s is null
return null
declare @s2 varchar(256)
set @s2 = ''
declare @l int
set @l = len(@s)
declare @p int
set @p = 1
while @p <= @l begin
declare @c int
set @c = ascii(substring(@s, @p, 1))
if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122
set @s2 = @s2 + char(@c)
set @p = @p + 1
end
if len(@s2) = 0
return null
return @s2
end
【问题讨论】:
-
可能加
or @c = 32 -
您使用的是哪个 dbms? (该代码是特定于产品的。)
-
微软 sql 服务器
-
Re:
set @l = len(@s)FromLen(): "返回指定字符串表达式的字符数,不包括尾随空格。"DataLength不排除尾随空格。对于 Unicode 字符串,您可以使用DataLength( UnicodeStringExpression ) / DataLength( N'#' )来获取字符长度。一般DataLength( Left( Coalesce( StringExpression, '#' ), 1 ) )会返回每个字符的字节数。 -
没有其他方法那么快,但在处理re-coding of accented characters to plain latin 时非常强大。您可能必须针对给定的排序规则调整这种方法...
标签: sql-server