2013-10-26 26 views
-1

我在同一個數據庫中有兩個表。在SQL Server中拆分一個長度爲nvarchar(120)的字符串而不會破壞單詞

表1,New_Company_Data它具有包含公司名稱的列「Company_Name」nvarchar(120)。

和Table2,Old_Company_data它有3列「Name1」nvarchar(40),「Name2」nvarchar(40)和「Name3」nvarchar(40)。

我想將數據從表New_Company_Data複製表Old_Company_Data而在此之前我必須拆分公司名稱這是在列「COMPANY_NAME」。我試圖分裂它,但它打破了話。

我怎樣才能在「COMPANY_NAME」字段列於下面的方式打破了字分割數據:

IF公司的名稱是< = 40,那麼請不要分裂。

如果公司名稱大於40並且< = 80那麼將它分成兩部分而不會破壞單詞。

如果公司名稱大於80且< = 120,則將其分成三部分而不會打破單詞。

我正在使用以下代碼,但沒有正確生成結果。例如,在下面的代碼中,我將string2命名爲「ik u」。但在這裏,我想獲得接下來的40個字符。

declare @strs nvarchar(120) 

set @strs = 'AlufinishGesellschaftfür Verfahrenstechnik u. zur 

Metalloberflächenbehandlung mbH & 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)<=40) set @lon = @palabras 
if (LEN(@strs)>40 AND LEN(@strs)<=80) set @lon = @palabras/2 --3 
if (LEN(@strs)>80 AND LEN(@strs)<=120) set @lon = @palabras/3 

--set @lon = @palabras/3 
set @contador = 1 
set @posicion = 0 
while @contador <= @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 <= @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 [email protected],[email protected] where id=12 
select @string1 as string1, @string2 as string2, @string3 as string3 
+1

這不是麥當勞,你不要在這裏點東西。我們可以幫助您解決您的問題,但首先向我們展示您嘗試的內容。 – gdoron

+0

是的,我寫了一個代碼,但它太大了,不能寫在這裏,我的意思是大於600個字符。或者,我可以在我的問題中編寫該代碼。我的第一天加入Stackoverflow – Kamran

+0

無論如何顯示它。 – gdoron

回答

0

如果您創建以下功能: -

create FUNCTION [dbo].WordWrap 
(
    @WrapAt int, 
    @Text nvarchar(1024) 
) 
RETURNS nvarchar(1024) 
AS 
BEGIN 
    declare @ReturnVaue nvarchar(1024);--the string to be passed back 
    declare @Snip int;-- the length to snip to the last space before the wrapap value 
    declare @Block int;-- the block number of the piece in the return string 
    set @Block=1;-- initialise the block number 
    set @Text=ltrim(rtrim(@Text));-- clean up the input string 
    while charindex(' ',@Text)>0 begin -- if there are any double spaces 
     set @Text=REPLACE(@Text,' ',' '); -- replace them with single spaces 
    end 
    if (@Text is null or DATALENGTH(@Text)<[email protected]) begin -- if the input string is null or short enough for 1 block 
     set @ReturnVaue='<1>'[email protected]+'</1>';-- load it into the return value and we're done 
    end else begin -- otherwise we have some work to do 
     set @ReturnVaue='' -- so let's initialise the return value 
     while DATALENGTH(@Text)>0 begin -- and keep going until we have finished 
      -- if the character after the wrapat is a space or there is a space anywhere before the wrapat 
      if SUBSTRING(@Text,@WrapAt+1,1)=' ' or CHARINDEX(' ',left(@Text,@WrapAt))>0 begin 
       if SUBSTRING(@Text,@WrapAt+1,1)=' ' begin -- if the character after the wrapat is a space 
        set @[email protected] we can snip to the wrapat 
       end else begin 
        --otherwise we have to snip to the last space before the wrapat 
        set @[email protected](' ',reverse(left(@text,@WrapAt))); 
       end 
       -- now we can load the return value with snipped text as the current block 
       set @ReturnVaue+='<'+CONVERT(varchar,@Block)+'>'+left(@Text,@Snip)+'</'+CONVERT(varchar,@Block)+'>'; 
       -- and leave just what's left to process, by jumping past the space (@Snip+2) 
       set @Text=SUBSTRING(@Text,@Snip+2,1024); 
      end else begin-- otherwise we have no space to split to - so we can only cut the string at wrapat 
       -- so we load the return value with the left hand wrapat characters as the current block 
       set @ReturnVaue+='<'+CONVERT(varchar,@Block)+'>'+LEFT(@Text,@WrapAt)+'</'+CONVERT(varchar,@Block)+'>'; 
       -- and leave just what's left to process, by jumping past the wrapat (@WrapAp+1) 
       set @Text=SUBSTRING(@Text,@WrapAt+1,1024); 
      end 
     set @Block+=1-- increment the block number in case we still have more work to do 
     end 
    end 
    RETURN @ReturnVaue; 
END 
go 

,你的表載有下面的測試數據: -

create table New_Company_Data (
    Company_name varchar(120) 
); 
go 
insert into New_Company_Data values (null); 
insert into New_Company_Data values (''); 
insert into New_Company_Data values (' abc abc abc  abc'); 
insert into New_Company_Data values ('a'); 
insert into New_Company_Data select REPLICATE('a',40)+REPLICATE('b',40)+REPLICATE('c',40) 
insert into New_Company_Data values ('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis vehicula, quam non lobortis molestie, purus dui porta sed.'); 
go 
select dbo.WordWrap(40,n.Company_name) 
from New_Company_Data n 

你會得到如下返回: -

NULL 
<1></1> 
<1>abc abc abc abc</1> 
<1>a</1> 
<1>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</1><2>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</2><3>cccccccccccccccccccccccccccccccccccccccc</3> 
<1>Lorem ipsum dolor sit amet, consectetur</1><2>adipiscing elit. Duis vehicula, quam non</2><3>lobortis molestie, purus dui porta sed.</3> 

I l讓您瞭解如何將(僞xml)字符串分解到您的目標列中,這取決於解決方案需要如何重複。如果它是一個關閉 - 將我的函數的輸出粘貼到輸出表中的臨時貨物列中,然後更新時序語句,將每個塊逐個拉出到所需的輸出列中。如果您需要經常重複此操作,請編寫一個可創建臨時表和光標的SP,並同時將這些塊移出到目標列中。顯然,你可以撕開我的代碼,並直接使用邏輯來形成一個動態更新語句(在每個塊從輸入字符串中剪切出來時,添加一個SET <目標字段>至<字符串值>,以便在每個枚舉。

我給你決定如何繼續(即使這是一個更直接的問題,給出一個想法,你想繼續)。

+1

非常感謝我非常感謝您的幫助 – Kamran

相關問題