问题是自我解释。你能否指出一种在字符串的每个大写字母之间加上空格的方法。
SELECT dbo.SpaceBeforeCap('ThisIsATestString')
会导致
This Is A Test String.
答案 0 :(得分:11)
仅当前一个和下一个字符为小写时才会添加空格。那样'MyABCAnalysis'将是'我的ABC分析'。
我也添加了对前一个空格的检查。由于我们的一些字符串以'GR_'为前缀,而某些字符串也包含下划线,因此我们可以使用replace函数,如下所示:
选择dbo.GR_SpaceBeforeCap(替换('GR_ABCAnalysis_Test','_','')) 返回'GR ABC分析测试'
CREATE FUNCTION GR_SpaceBeforeCap (
@str nvarchar(max)
)
returns nvarchar(max)
as
begin
declare
@i int, @j int
, @cp nchar, @c0 nchar, @c1 nchar
, @result nvarchar(max)
select
@i = 1
, @j = len(@str)
, @result = ''
while @i <= @j
begin
select
@cp = substring(@str,@i-1,1)
, @c0 = substring(@str,@i+0,1)
, @c1 = substring(@str,@i+1,1)
if @c0 = UPPER(@c0) collate Latin1_General_CS_AS
begin
-- Add space if Current is UPPER
-- and either Previous or Next is lower
-- and Previous or Current is not already a space
if @c0 = UPPER(@c0) collate Latin1_General_CS_AS
and (
@cp <> UPPER(@cp) collate Latin1_General_CS_AS
or @c1 <> UPPER(@c1) collate Latin1_General_CS_AS
)
and @cp <> ' '
and @c0 <> ' '
set @result = @result + ' '
end -- if @co
set @result = @result + @c0
set @i = @i + 1
end -- while
return @result
end
答案 1 :(得分:7)
假设SQL Server 2005或更高版本,这是根据此处的代码修改的:http://www.kodyaz.com/articles/case-sensitive-sql-split-function.aspx
CREATE FUNCTION SpaceBeforeCap
(
@str nvarchar(max)
)
returns nvarchar(max)
as
begin
declare @i int, @j int
declare @returnval nvarchar(max)
set @returnval = ''
select @i = 1, @j = len(@str)
declare @w nvarchar(max)
while @i <= @j
begin
if substring(@str,@i,1) = UPPER(substring(@str,@i,1)) collate Latin1_General_CS_AS
begin
if @w is not null
set @returnval = @returnval + ' ' + @w
set @w = substring(@str,@i,1)
end
else
set @w = @w + substring(@str,@i,1)
set @i = @i + 1
end
if @w is not null
set @returnval = @returnval + ' ' + @w
return ltrim(@returnval)
end
然后可以像上面建议的那样调用它。
答案 2 :(得分:3)
此功能结合了以前的答案。选择性地选择保留相邻的CAPS:
CREATE FUNCTION SpaceBeforeCap (
@InputString NVARCHAR(MAX),
@PreserveAdjacentCaps BIT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE
@i INT, @j INT,
@previous NCHAR, @current NCHAR, @next NCHAR,
@result NVARCHAR(MAX)
SELECT
@i = 1,
@j = LEN(@InputString),
@result = ''
WHILE @i <= @j
BEGIN
SELECT
@previous = SUBSTRING(@InputString,@i-1,1),
@current = SUBSTRING(@InputString,@i+0,1),
@next = SUBSTRING(@InputString,@i+1,1)
IF @current = UPPER(@current) COLLATE Latin1_General_CS_AS
BEGIN
-- Add space if Current is UPPER
-- and either Previous or Next is lower or user chose not to preserve adjacent caps
-- and Previous or Current is not already a space
IF @current = UPPER(@current) COLLATE Latin1_General_CS_AS
AND (
@previous <> UPPER(@previous) COLLATE Latin1_General_CS_AS
OR @next <> UPPER(@next) collate Latin1_General_CS_AS
OR @PreserveAdjacentCaps = 0
)
AND @previous <> ' '
AND @current <> ' '
SET @result = @result + ' '
END
SET @result = @result + @current
SET @i = @i + 1
END
RETURN @result
END
GO
SELECT dbo.SpaceBeforeCap('ThisIsASampleDBString', 1)
GO
SELECT dbo.SpaceBeforeCap('ThisIsASampleDBString', 0)
答案 3 :(得分:1)
CLR和正则表达式或26个替换语句区分大小写的collate子句和trim。
答案 4 :(得分:1)
另一种策略是检查每个字符的ascii值:
create function SpaceBeforeCap
(@str nvarchar(max))
returns nvarchar(max)
as
begin
declare @result nvarchar(max)= left(@str, 1),
@i int = 2
while @i <= len(@str)
begin
if ascii(substring(@str, @i, 1)) between 65 and 90
select @result += ' '
select @result += substring(@str, @i, 1)
select @i += 1
end
return @result
end
/***
SELECT dbo.SpaceBeforeCap('ThisIsATestString')
**/
答案 5 :(得分:0)
虽然我真的很喜欢char循环答案,但我对性能并不感到兴奋。对于我的用例,我发现它只花费很少的时间。
CREATE function SpaceBeforeCap
(@examine nvarchar(max))
returns nvarchar(max)
as
begin
DECLARE @index as INT
SET @index = PatIndex( '%[^ ][A-Z]%', @examine COLLATE Latin1_General_BIN)
WHILE @index > 0 BEGIN
SET @examine = SUBSTRING(@examine, 1, @index) + ' ' + SUBSTRING(@examine, @index + 1, LEN(@examine))
SET @index = PatIndex( '%[^ ][A-Z]%', @examine COLLATE Latin1_General_BIN)
END
RETURN LTRIM(@examine)
end
这利用了以下事实:
case sensitive pattern search only works in some collations。字符类[^ ]
表示空格以外的任何内容,因此,当我们添加缺少的空格时,我们将进一步匹配字符串,直到完成为止。