如何拆分非结构化数据并将其插入表中

时间:2017-06-07 06:41:07

标签: sql sql-server

如何拆分非结构化数据并将其插入表

以下数据表示非结构化数据,我需要在一组不同的列中进行格式化并将其插入表中,

ABB0512  709023378812005327020220953171        025336141992033270207033123002663270200401ABC02055       ABC02055CBL MURARJIPET,MALAD MAlAD      MHIN3270204     ABC                                                          333000000000050000000000000050000000000000000000333000000000050000000000000000000000000000000000   000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000

ABB0222  709023378812005327020220953171    025222141992033270207033123002663270200401PQR02055       PQR02055CBL MURARJIPET,THANE THANE      MHIN3270204     PQR                                                          222000000000010000000000000010000000000000000000222000000000010000000000000000000000000000000000   000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000

格式化后的数据结构如下所示:

----a---------b------------c---------------------d-----e
========================================================================
ABC02055    ABC02055    CBL MURARJIPET,MALAD    MAlAD   MHIN

PQR02055    PQR02055    CBL MURARJIPET,THANE    THANE   MHIN

以下查询仅给出了第一个字符串的预期结果

Declare @variablestring varchar(max)
set @variablestring = substring(@strval,0,407)
print @variablestring
INSERT INTO tbldummyEntries(A, B, C,D,E,F)
values( CAST(SUBSTRING(@variablestring, 84, 15) AS Varchar(MAX)) ,
    CAST(SUBSTRING(@variablestring, 99, 8) AS Varchar(MAX)) ,
    CAST(SUBSTRING(@variablestring, 107, 22) AS Varchar(MAX)),
    CAST (SUBSTRING(@variablestring, 130, 13) AS Varchar(MAX)),
    CAST (SUBSTRING (@variablestring, 143, 5) AS Varchar(MAX)),
    CAST (SUBSTRING(@variablestring , 148 , 261) AS Varchar(MAX)))
set @strval=REPLACE(@strval,@variablestring,'')

我如何为第一个之后的条目复制它?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我尝试如下:(根据您的格式保持循环长度)

Declare @variablestring varchar(max),@strval varchar(max)
set @strval='ABB0512  709023378812005327020220953171        025336141992033270207033123002663270200401ABC02055       ABC02055CBL MURARJIPET,MALAD MAlAD      MHIN3270204     ABC                                                          333000000000050000000000000050000000000000000000333000000000050000000000000000000000000000000000   000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000

ABB0222  709023378812005327020220953171    025222141992033270207033123002663270200401PQR02055       PQR02055CBL MURARJIPET,THANE THANE      MHIN3270204     PQR                                                          222000000000010000000000000010000000000000000000222000000000010000000000000000000000000000000000   000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000'
set @variablestring = substring(@strval,0,407)
while len(@strval)>406
begin
INSERT INTO tbldummyEntries(A, B, C,D,E,F) values
select CAST(SUBSTRING(@variablestring, 84, 15) AS Varchar(MAX)) ,
    CAST(SUBSTRING(@variablestring, 99, 8) AS Varchar(MAX)) ,
    CAST(SUBSTRING(@variablestring, 107, 22) AS Varchar(MAX)),
    CAST (SUBSTRING(@variablestring, 130, 13) AS Varchar(MAX)),
    CAST (SUBSTRING (@variablestring, 143, 5) AS Varchar(MAX)),
    CAST (SUBSTRING(@variablestring , 148 , 261) AS Varchar(MAX))
set @strval=REPLACE(@strval,@variablestring,'')
set @variablestring = substring(@strval,0,407)
    end