Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit a string of length nvarchar(120) without breaking words in SQL Server
    primarykey
    data
    text
    <p>I have two tables in a same database. </p> <p>Table1, <strong>New_Company_Data</strong> it has column "Company_Name" nvarchar(120) which contains name of companies. </p> <p>and Table2, <strong>Old_Company_data</strong> it has 3 column "Name1" nvarchar(40), "Name2" nvarchar(40) and "Name3" nvarchar(40). </p> <p>I want to copy the data from table <strong>New_Company_Data</strong> to the table <strong>Old_Company_Data</strong> but before this I have to split the companies name which are in column "Company_Name". I tried to split it but its breaking the words. </p> <p>How can I split the data in "Company_Name" field with out breaking the words in following way: </p> <p>IF company name is &lt;=40 then don't split. </p> <p>IF company name is >40 and &lt;=80 then split it in two parts without breaking the words.</p> <p>IF company name is >80 and &lt;=120 then split it in three parts without breaking the words. </p> <p>I am using following code but its not generating the results properly. E.g in the below code i am getting string2 as "ik u." But here I want to get next 40 characters. </p> <pre><code>declare @strs nvarchar(120) set @strs = 'AlufinishGesellschaftfür Verfahrenstechnik u. zur Metalloberflächenbehandlung mbH &amp; C' --40 declare @lon int declare @palabras int declare @contador int declare @posicion int declare @string1 nvarchar(40) declare @string2 nvarchar(40) declare @string3 nvarchar(40) --wordcount set @palabras = LEN(@strs)-LEN(replace(@strs, ' ', ''))+1 --3 --words per batch if (LEN(@strs)&lt;=40) set @lon = @palabras if (LEN(@strs)&gt;40 AND LEN(@strs)&lt;=80) set @lon = @palabras/2 --3 if (LEN(@strs)&gt;80 AND LEN(@strs)&lt;=120) set @lon = @palabras/3 --set @lon = @palabras/3 set @contador = 1 set @posicion = 0 while @contador &lt;= @lon begin -- search for the first batch set @posicion = CHARINDEX(' ',@strs,@posicion+1)-- here it will found 1st space set @contador = @contador+1 end set @string1 = Left(@strs, @posicion) set @strs = replace(@strs, @string1, '') set @contador = 1 set @posicion = 0 while @contador &lt;= @lon begin -- search for the second batch set @posicion = CHARINDEX(' ',@strs,@posicion+1) set @contador = @contador+1 end set @string2 = LEFT(@strs, @posicion) set @string3 = replace(@strs, @string2, '') --use test --update company_backup --set company1=@string1,company2=@string3 where id=12 select @string1 as string1, @string2 as string2, @string3 as string3 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload