Declare @S Nvarchar(100)
Select @S = N'发行千亿国债解决高校债务'
Select TOP 100 ID = Identity(Int, 0, 1) Into #T From SysColumns A, SysObjects B
Select Substring(@S, A.ID * B.ID + 1, B.ID) From #T A, #T B Where A.ID <= Len(@S) / B.ID - 1 And B.ID Between 2 And Len(@S) / 2 + 1
Drop Table #T
--Result
/*
发行
千亿
国债
解决
高校
债务
发行千
亿国债
解决高
校债务
发行千亿
国债解决
高校债务
发行千亿国
债解决高校
发行千亿国债
解决高校债务
发行千亿国债解
*/
Select @S = N'发行千亿国债解决高校债务'
Select TOP 100 ID = Identity(Int, 0, 1) Into #T From SysColumns A, SysObjects B
Select Substring(@S, A.ID * B.ID + 1, B.ID) From #T A, #T B Where A.ID <= Len(@S) / B.ID - 1 And B.ID >= 2 And B.ID <= Len(@S) / 2 + 1 And B.ID<= 7
Drop Table #T
--Result
/*
发行
千亿
国债
解决
高校
债务
发行千
亿国债
解决高
校债务
发行千亿
国债解决
高校债务
发行千亿国
债解决高校
发行千亿国债
解决高校债务
发行千亿国债解
*/
as
begin
declare @t table(id int identity,string varchar(8000))
while len(@sql) >= @unit_len
begin
insert @t(string)
select substring(@sql,1,@unit_len) set @sql = stuff(@sql,1,@unit_len,'')
end
select string from @t order by id
end
go
create proc p_cut @sql varchar(8000)
as
begin
create table #t(id int identity,string varchar(8000))
declare @i int set @i = 2
while @i <= len(@sql)/2+1
begin
insert #t(string) exec p_cut_sql @i,@sql
set @i = @i + 1
end
select * from #t order by id
end
go
exec p_cut '发行千亿国债解决高校债务'
go
drop proc p_cut_sql,p_cut
/*
id string
----------- --------------
1 发行
2 千亿
3 国债
4 解决
5 高校
6 债务
7 发行千
8 亿国债
9 解决高
10 校债务
11 发行千亿
12 国债解决
13 高校债务
14 发行千亿国
15 债解决高校
16 发行千亿国债
17 解决高校债务
18 发行千亿国债解(18 row(s) affected)*/
drop function fnSplit
GO
create function fnSplit(@str varchar(8000))
returns @t table(name varchar(8000))
as
begin
declare @string varchar(100),@strbk varchar(8000),@nLen int,@i int
set @strbk = @str
set @string = ''
set @nLen = len(@str)
set @i = 2
while @i <= @nLen
begin
set @str = @strbk
while len(@str) >= @i
begin
set @string = substring(@str,1,@i)
set @str = stuff(@str,1,@i,'')
insert @t select @string
end
set @i = @i + 1
end
return
end
GO
----查询
declare @str varchar(8000)
set @str = '发行千亿国债解决高校债务'
select * from dbo.fnSplit(@str)----清楚测试函数
drop function fnSplit/*结果
name
--------------------------------------------
发行
千亿
国债
解决
高校
债务
发行千
亿国债
解决高
校债务
发行千亿
国债解决
高校债务
发行千亿国
债解决高校
发行千亿国债
解决高校债务
发行千亿国债解
发行千亿国债解决
发行千亿国债解决高
发行千亿国债解决高校
发行千亿国债解决高校债
发行千亿国债解决高校债务(所影响的行数为 23 行)
*/
if object_id('fnSplit') is not null
drop function fnSplit
GO
create function fnSplit(@str varchar(8000))
returns @t table(name varchar(8000))
as
begin
declare @string varchar(100),@strbk varchar(8000),@nLen int,@i int
set @strbk = @str
set @string = ''
set @nLen = len(@str)
set @i = 2
while @i <= @nLen/2 + 1
begin
set @str = @strbk
while len(@str) >= @i
begin
set @string = substring(@str,1,@i)
set @str = stuff(@str,1,@i,'')
insert @t select @string
end
set @i = @i + 1
end
return
end
GO
----查询
declare @str varchar(8000)
set @str = '发行千亿国债解决高校债务'
select * from dbo.fnSplit(@str)----清楚测试函数
drop function fnSplit/*结果
name
--------------------------------------------
发行
千亿
国债
解决
高校
债务
发行千
亿国债
解决高
校债务
发行千亿
国债解决
高校债务
发行千亿国
债解决高校
发行千亿国债
解决高校债务
发行千亿国债解(所影响的行数为 18 行)
*/
declare @t int
declare @i int
declare @j intset @str = N'发行千亿国债解决高校债务'
set @t = len(@str)
set @i = 2
set @j = 1
while @i<@t
begin while @j+@i <=@t+1
begin
select substring(@str,@j,@i) as name
set @j = @j + @i
endset @i = @i +1
set @j = 1
end