Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
williamku87
Starting Member
6 Posts |
Posted - 2011-06-27 : 04:34:21
|
| [code]declare @desc nvarchar(100)declare @break intdeclare @loop intdeclare @output nvarchar(200)set @break = 10set @desc = 'this is a test, every 10 character will break'set @loop = 0set @output = ''while @loop <= LEN(@desc)/@breakbeginset @output = @output + SUBSTRING(@desc, (@loop*@break)+1, @break) + '<br>'set @loop = @loop + 1endselect @output[/code]output isthis is a <br>test, ever<br>y 10 chara<br>cter will <br>break<br>but i need to add extra checking to make sure the word itself doesnt break.output should be like this this is a <br>test, every<br> 10 character<br> will break<br> |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-06-27 : 04:57:59
|
what if you have a verylongword in the string ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-27 : 05:01:51
|
| This should give you an idea. Needs some tidying up for the end of string probablyAlso do you want spaces replaced by <br> or as well as.WHat about punctuation? Would probably need patindex instead or charindex for that.Could also be made more efficient if neededdeclare @desc nvarchar(100)declare @break intdeclare @pos intdeclare @spacepos intdeclare @output nvarchar(200)declare @tmp varchar(200)set @break = 10set @desc = 'this is a test, every 10 characterddddddddddd will break'set @pos = 0set @output = ''while @pos < LEN(@desc)beginset @tmp = SUBSTRING(@desc, @pos+1, @break+1)set @spacepos = charindex(' ',reverse(@tmp))if @spacepos = 0 set @tmp = LEFT(@tmp,@break)else set @tmp = LEFT(@tmp,@break+1 - @spacepos)set @output = @output + @tmp + '<br>'set @pos = @pos + LEN(@tmp)+1endselect @outputthis is a<br>test,<br>every 10<br>characterd<br>ddddddddd<br>will <br>break<br>==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
williamku87
Starting Member
6 Posts |
Posted - 2011-06-27 : 05:02:29
|
quote: Originally posted by khtan what if you have a verylongword in the string ? KH[spoiler]Time is always against us[/spoiler]
actually i will set my limit, one row is 50.so one word can't hv 50 length. |
 |
|
|
williamku87
Starting Member
6 Posts |
Posted - 2011-06-27 : 05:06:22
|
quote: Originally posted by nigelrivett This should give you an idea. Needs some tidying up for the end of string probablyAlso do you want spaces replaced by <br> or as well as.WHat about punctuation? Would probably need patindex instead or charindex for that.Could also be made more efficient if neededdeclare @desc nvarchar(100)declare @break intdeclare @pos intdeclare @spacepos intdeclare @output nvarchar(200)declare @tmp varchar(200)set @break = 10set @desc = 'this is a test, every 10 characterddddddddddd will break'set @pos = 0set @output = ''while @pos < LEN(@desc)beginset @tmp = SUBSTRING(@desc, @pos+1, @break+1)set @spacepos = charindex(' ',reverse(@tmp))if @spacepos = 0 set @tmp = LEFT(@tmp,@break)else set @tmp = LEFT(@tmp,@break+1 - @spacepos)set @output = @output + @tmp + '<br>'set @pos = @pos + LEN(@tmp)+1endselect @outputthis is a<br>test,<br>every 10<br>characterd<br>ddddddddd<br>will <br>break<br>==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
i will try. thanks for ur idea. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-27 : 05:06:26
|
| The code I posted splits any long words too. Depends on where your data is coming from but it's dangerous to rely on words being valid.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|