34,576
社区成员
发帖
与我相关
我的任务
分享
DECLARE @s VARCHAR(4000)
SELECT @s='aaa bbb ccccccc ddd eee fff ggggggg hh ii kk jjjjjjjj z sddddd efffff'
WHILE CHARINDEX(' ',@s)>0
BEGIN
IF SUBSTRING(@s,10,1)!='' AND SUBSTRING(@s,11,1)!=''
BEGIN
PRINT LEFT(@s,10-CHARINDEX(' ',REVERSE(LEFT(@s,10))))
SET @s=STUFF(@s,1,11-CHARINDEX(' ',REVERSE(LEFT(@s,10))),'')
END
ELSE
BEGIN
PRINT LEFT(@s,10)
SET @s=LTRIM(RIGHT(@s,LEN(@s)-10))
END
END
PRINT @s
/*
aaa bbb
ccccccc
ddd eee
fff
ggggggg hh
ii kk
jjjjjjjj z
sddddd
*/
declare @input varchar(4000),@s varchar(20),@i int
select @input='You have to finish all what need to do today'
while @input>''
begin
set @i=10
while substring(@input,@i+1,1)>''
select @i=@i+1
set @s=left(@input,@i)
print @s
select @input=stuff(@input,1,@i,'')
end
declare @input varchar(4000),@s varchar(20),@i int
select @input='You have to finish all what need to do today'
while @input>''
begin
set @i=10
while substring(@input,@i,1)>''
select @i=@i+1, @s=left(@input,@i)
print @s
select @input=stuff(@input,1,@i,'')
end
/*
You have
to finish
all what
need to
do today
*/
declare @input nvarchar(4000)
set @input='You have to finish all what need to do today'
declare @c int
while len(@input) > 10
begin
set @c = charindex(' ', @input)
while 1 = 1
begin
if charindex(' ', @input, @c + 1) > 11 or charindex(' ', @input, @c + 1) = 0 break
set @c = charindex(' ', @input, @c + 1)
end
print left(@input, @c - 1)
set @input = right(@input, len(@input) - @c)
end
if @input <> '' print @input
declare @input varchar(4000)
select @input='You have to finish all what need to do today'
while @input>''
begin
print left(@input,10)
select @input=stuff(@input,1,10,'')
end