举例:@input='You have to finish all what need to do today' 以10个字符为一行输出,测为:
'You have' 'to finish' 'all what' 'need to do' 'today'
大概就是这个意思。请高手过招.....
目前最佳答案在7楼代码如下:
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 */
/******************************************//*回复:代码20080516004 总:00000000018 *//*主题:自动换行(不支持汉字和单个单词过长) *//*作者:二等草 *//******************************************//************例子数据 begin****************/DECLARE@sVARCHAR(4000)
SELECT@s='aaa bbb cc c大家c d的得到dd e fff ggggggg hh ii kk jjjjjjjj z sddddd efffff'/************例子数据 end******************//************代码 begin***************/declare@iint,@jint,@bxint,@lintselect@bx=1,@l=10,@i=0,@s=@s+''while@bx<len(@s)
beginselect@j=charindex('',@s,@bx)
if@j-@i>@lbeginprintsubstring(@s,@i,@bx-@i)
select@i=@bxendset@bx=@j+1endprintright(@s,@bx-@i)
/************代码 end*****************//************结果 begin***************
aaa bbb
cc c大家c
d的得到dd e
fff
ggggggg hh
ii kk
jjjjjjjj z
sddddd
efffff
************结果 end*****************//************清除*************************/
/******************************************//*回复:代码20080516004 总:00000000018 *//*主题:自动换行(不支持汉字,支持单个单词过长)*//*作者:二等草 *//******************************************//************例子数据 begin****************/DECLARE@sVARCHAR(4000)
SELECT@s='sds abcdefghijklmn cc c大家c d的得到缩短簌阿撒阿撒簌dd e fff ggggggg hh ii kk jjjjjjjj z sddddd efffff'/************例子数据 end******************//************代码 begin***************/declare@iint,@jint,@bxint,@lintselect@bx=1,@l=10,@i=1,@s=@s+''while@bx<len(@s)
beginselect@j=charindex('',@s,@bx)
if@j-@i>@lbeginprintsubstring(@s,@i,@bx-@i)
select@i=@bxendif@j-@bx>@lset@bx=@bx+@lelseset@bx=@j+1endprintright(@s,@bx-@i)
/************代码 end*****************//************结果 begin***************
sds
abcdefghij
klmn cc
c大家c
d的得到缩短簌阿撒阿
撒簌dd e fff
ggggggg hh
ii kk
jjjjjjjj z
sddddd
efffff
************结果 end*****************//************清除*************************/
DECLARE @s VARCHAR(4000) set @s='aa a d dasa dsfsdfsgaasfsdaf'
declare @c int while len(@s) > 10 begin set @c = charindex(' ', @s) while 1 = 1 begin if charindex(' ', @s, @c + 1) > 11 or charindex(' ', @s, @c + 1) = 0 break set @c = charindex(' ', @s, @c + 1) end if @c=0 --因为最后一个英文过长而又取不到空格导致无法@c永久为0,会导致存储过程死循环,于是强制附值 开始 set @c=11 print left(@s, @c - 1) set @s = right(@s, len(@s) - @c) end if @s <> '' print @s
DECLARE @s VARCHAR(4000) set @s='aa a d dasa dsfsdfsgaasfsdaf'
declare @c int while len(@s) > 10 and charindex(' ',@s)>0 begin set @c = charindex(' ', @s) while 1 = 1 begin if charindex(' ', @s, @c + 1) > 11 or charindex(' ', @s, @c + 1) = 0 break set @c = charindex(' ', @s, @c + 1) end print left(@s, @c - 1) set @s = right(@s, len(@s) - @c) end if @s <> '' print @s