如何在字段之后分隔中间名/首字母,并且应该在插入之前/之后完成

时间:2012-07-20 14:55:32

标签: sql sql-server-2008 parsing insert

我有下表:

InventorLast    InventorFirst
Bram            Ernst Arne Johan
Gesing          Ernst Rudolf F.
Beranek         Ernst W.
Koch            Ernst-Christian
Bozak           Erol
Kurtas          Erozan Mehmet
Calder          Errol Jay
Binder          Erwin
Goetschi        Erwin

我想在第一个空格中分隔中间名/首字母或 - 如Ernst-Christian示例中所示。

在创建表格或从源数据插入后,我会更容易这样做吗?

现有的插入序列如下:

insert into Inventor (PatentNo, InventorFirst, InventorLast, City, statename, country, NationalityCountry, ResidenceCountry)
select PatentNo, InventorFirstname, InventorLastname, City, statename, country, NationalityCountry, ResidenceCountry
from InventorUpdate
where InventorFirstName is not null 
and InventorLastName is not null

我知道我必须添加InventorMiddle列。我只需要一些帮助 编码。

谢谢!

1 个答案:

答案 0 :(得分:1)

相当难看,但是......

认为你只是在没有“没有空间无名”的情况下没有说要做什么

select substring(InventorFirst, 0, (case when  
                 patindex('%[ -]%', InventorFirst) = 0
                 then len(InventorFirst) + 1
                 else patindex('%[ -]%', InventorFirst)
                 end)
                 ) as middleValue,
       case 
         when patindex('%[ -]%', InventorFirst) = 0
          then '' 
          else substring(InventorFirst, 
                         patindex('%[ -]%', InventorFirst)+1, 
                         len(InventorFirst))
       end as endValue 
                 from inventor

http://sqlfiddle.com/#!3/57fa7/20