如何从数字列表中替换单个数字(来自一组行)?

时间:2015-09-15 08:50:24

标签: python

我是数据库的新手,我想在我的数据库中这样做。 我应该使用word 2007或Python还是其他东西?我目前只有这两件事。

原始信息是这样的:
你好,你怎么反对= 13515
ofdifaoididpodvjakjeoijknvkln
ikvnoivwoeij,VN,nviojv
nkavjoiewgkjvlkaior

此邮件的号码将被此列表中的号码替换

876191
11166524个
1117225个
1116953个
798993个
793519个
1116737个
1116691个
1116364个
1115635个
909014个
760195个
689981个
604787个
1116217个
1104482个
869990个
886290个
1115893个

并且该消息应该多次复制本身,因为该列表中有数字。

所以我的结果应该是

你怎么回答= 876191
ofdifaoididpodvjakjeoijknvkln
ikvnoivwoeij,VN,nviojv
nkavjoiewgkjvlkaior
你好,你怎么回答= 11166524
ofdifaoididpodvjakjeoijknvkln
ikvnoivwoeij,VN,nviojv
nkavjoiewgkjvlkaior
你好,你怎么回答= 1117225
ofdifaoididpodvjakjeoijknvkln
ikvnoivwoeij,VN,nviojv
nkavjoiewgkjvlkaior
等等
请注意,列表中的数字是6或7或8位数字

谢谢你

1 个答案:

答案 0 :(得分:1)

这是一个有趣的问题,它可以在sql server中完成。

首先我将原始邮件放入表中 - @messages_original。 它没有必要成为一张桌子,因为只有一张桌子。

    set nocount on

    declare @messages_original table ( i int not null identity(1,1) primary key clustered, mess varchar(1008) not null)

    declare @results table ( i int not null identity(1,1) primary key clustered, mess varchar(1008) not null)

    declare @numbers table ( i int not null identity(1,1) primary key clustered, num varchar(10) not null)

    declare @i int 
    declare @z int
    declare @number_ varchar(10) --- the number in the original message
    declare @number  varchar(10) --- the number that needs to be replace the original number and save to the @results

    insert into @messages_original values ('hello how are you ref=13515
    ofdifaoididpodvjakjeoijknvkln
    ikvnoivwoeij,vn,nviojv
    nkavjoiewgkjvlkaior')

     --I add the numbers into another table.

    insert into @numbers (num) values ('876191')
    insert into @numbers (num) values ('11166524')
    insert into @numbers (num) values ('1117225')
    insert into @numbers (num) values ('1116953')
    insert into @numbers (num) values ('798993')
    insert into @numbers (num) values ('793519')
    insert into @numbers (num) values ('1116737')
    insert into @numbers (num) values ('1116691')
    insert into @numbers (num) values ('1116364')
    insert into @numbers (num) values ('1115635')
    insert into @numbers (num) values ('909014')
    insert into @numbers (num) values ('760195')
    insert into @numbers (num) values ('689981')
    insert into @numbers (num) values ('604787')
    insert into @numbers (num) values ('1116217')
    insert into @numbers (num) values ('1104482')
    insert into @numbers (num) values ('869990')
    insert into @numbers (num) values ('886290')
    insert into @numbers (num) values ('1115893')

select @i = 1,
       @z = count(*) from @numbers 

select @number_ =  SubString(mess,PATINDEX('%[0-9]%',mess),Len(mess)),
       @number_ =  SubString(@number_,0,PATINDEX('%[^0-9]%',@number_))
from
@messages_original 
where i = 1


while (@i <= @z) begin

        select @number = num from @numbers where i = @i

        insert into @results(mess)
        select replace(mess,@number_,@number)
        from
        @messages_original 
        where i = 1



select @i = @i+ 1

end /*while*/


select * from @results

enter image description here