Technical Article

Add carriage return every nth char

,

This script will take a string (8000) and add a carriage return at the closest space to nth char. e.g. You only want 50 characters per line. This will add carriage return at the closest space before the 50th character.

Declare
@vStr1Varchar(8000),
@vStr2Varchar(8000),
@vStr3Varchar(8000),
@vLengthLimitInt

Set NoCOunt On

Set @vStr1 = 'representative to the International Atomic Energy Agency, said enriched uranium has been found at the Kalay-e Electric Co., just west of Tehran. Salehi, speaking on Tehran television, ruled out that the enriched uranium found at the site and another facility at Natanz was produced in Iran. Foreign diplomats said last week that IAEA inspectors found minute quantities of weapons-grade uranium at the Kalay-e Electric Co. Earlier this year, U.N. inspectors found weapons-grade highly enriched uranium particles at a plant in Natanz that is supposed to produce only a lower grade for energy purposes. Salehi said Iranian and IAEA officials were surprised that high percentages of enriched uranium had been found at both sites.'
Set @vLengthLimit = 40
Set @vStr2 = ''
Set @vStr3 = ''

While DataLength(@vStr1) > 0
Begin
If (Len(@vStr1) + Len(@vStr2)) <= @vLengthLimit
Begin
Set @vStr3 = @vStr3 + @vStr2 + @vStr1
Break;
End

Set @vStr2 = @vStr2 + SubString(@vStr1, 1, 1)
Set @vStr1 = SubString(@vStr1, 2, 8000)
If Len(@vStr2) >= @vLengthLimit AND ASCII(REVERSE(@vStr2)) = 32
Begin
Set @vStr3 = @vStr3 + RTRIM(LTRIM(@vStr2)) + CHAR(13)
Set @vStr2 = ''
End
End

Print '|' + @vStr3 + '|'

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating