0

我有一个表列,其中包含一组地址作为 XML 数据:

<?xml version="1.0" encoding="utf-16"?>
<AddressBook>
   <Address>
      <ID>-1</ID>
      <AddressID>PRIMARY</AddressID>
      <FirstName>JOHN L.</FirstName>
      <LastName>JOHSON/JOHN L.</LastName>
      <StreetLine1>123 NOWHERE ST</StreetLine1>
      <City>OVERHERE</City>
      <StateName>CA</StateName>
      <StateCode>5</StateCode>
      <PostalCode>12345</PostalCode>
      <CountryName>United States</CountryName>
      <CountryCode>en-US</CountryCode>
      <PhoneNumber>5551231234</PhoneNumber>
      <PhoneNumber2>5551231234</PhoneNumber2>
      <TaxScheduleID>CA</TaxScheduleID>
      <ShowAddress>1</ShowAddress>
   </Address>
   <Address>
      <ID>-1</ID>
      <AddressID>SECONDARY</AddressID>
      <FirstName>SAM</FirstName>
      <LastName>LARSON/SAM</LastName>
      <StreetLine1>456 OVERHERE</StreetLine1>
      <City>NOTTHERE</City>
      <StateName>CA</StateName>
      <StateCode>5</StateCode>
      <PostalCode>54321</PostalCode>
      <CountryName>United States</CountryName>
      <CountryCode>en-US</CountryCode>
      <PhoneNumber>5553334444</PhoneNumber>
      <PhoneNumber2>5553334444</PhoneNumber2>
      <TaxScheduleID>CA</TaxScheduleID>
      <ShowAddress>1</ShowAddress>
   </Address>
</AddressBook>

我的问题是 last name 元素包含与 '/' 前面的第一个命名的不必要的重复:

<FirstName>JOHN L.</FirstName>
<LastName>JOHSON/JOHN L.</LastName>

如何删除姓氏元素中的“/”和“/”后面的每个字符,以便创建这样的结果?

<FirstName>JOHN L.</FirstName>
<LastName>JOHSON</LastName>
4

2 回答 2

1

尝试使用charindex

Select Left('JOHSON/JOHN L.', CHARINDEX('/', 'JOHSON/JOHN L.') - 1)
于 2012-12-06T19:40:59.880 回答
1

具有包含 2 条记录的表变量的完整工作示例:

declare @t table (xmldata xml);
insert @t values ('
<AddressBook>
   <Address>
      <ID>-1</ID>
      <AddressID>PRIMARY</AddressID>
      <FirstName>JOHN L.</FirstName>
      <LastName>JOHSON/JOHN L.</LastName>
      <StreetLine1>123 NOWHERE ST</StreetLine1>
      <City>OVERHERE</City>
      <StateName>CA</StateName>
      <StateCode>5</StateCode>
      <PostalCode>12345</PostalCode>
      <CountryName>United States</CountryName>
      <CountryCode>en-US</CountryCode>
      <PhoneNumber>5551231234</PhoneNumber>
      <PhoneNumber2>5551231234</PhoneNumber2>
      <TaxScheduleID>CA</TaxScheduleID>
      <ShowAddress>1</ShowAddress>
   </Address>
   <Address>
      <ID>-1</ID>
      <AddressID>SECONDARY</AddressID>
      <FirstName>SAM</FirstName>
      <LastName>LARSON/SAM</LastName>
      <StreetLine1>456 OVERHERE</StreetLine1>
      <City>NOTTHERE</City>
      <StateName>CA</StateName>
      <StateCode>5</StateCode>
      <PostalCode>54321</PostalCode>
      <CountryName>United States</CountryName>
      <CountryCode>en-US</CountryCode>
      <PhoneNumber>5553334444</PhoneNumber>
      <PhoneNumber2>5553334444</PhoneNumber2>
      <TaxScheduleID>CA</TaxScheduleID>
      <ShowAddress>1</ShowAddress>
   </Address>
</AddressBook>');
insert @t values ('
<AddressBook>
   <Address>
      <ID>-1</ID>
      <AddressID>PRIMARY</AddressID>
      <FirstName>JANE L.</FirstName>
      <LastName>JOHSON/JANE L.</LastName>
      <StreetLine1>123 NOWHERE ST</StreetLine1>
      <City>OVERHERE</City>
      <StateName>CA</StateName>
      <StateCode>5</StateCode>
      <PostalCode>12345</PostalCode>
      <CountryName>United States</CountryName>
      <CountryCode>en-US</CountryCode>
      <PhoneNumber>5551231234</PhoneNumber>
      <PhoneNumber2>5551231234</PhoneNumber2>
      <TaxScheduleID>CA</TaxScheduleID>
      <ShowAddress>1</ShowAddress>
   </Address>
   <Address>
      <ID>-1</ID>
      <AddressID>SECONDARY</AddressID>
      <FirstName>NIGEL</FirstName>
      <LastName>NIGEL JR./NIGEL</LastName>
      <StreetLine1>456 OVERHERE</StreetLine1>
      <City>NOTTHERE</City>
      <StateName>CA</StateName>
      <StateCode>5</StateCode>
      <PostalCode>54321</PostalCode>
      <CountryName>United States</CountryName>
      <CountryCode>en-US</CountryCode>
      <PhoneNumber>5553334444</PhoneNumber>
      <PhoneNumber2>5553334444</PhoneNumber2>
      <TaxScheduleID>CA</TaxScheduleID>
      <ShowAddress>1</ShowAddress>
   </Address>
</AddressBook>');

   update t
        set xmldata =
    (select n.a.value('ID[1]','nvarchar(max)') ID,
            n.a.value('AddressID[1]','nvarchar(max)') AddressID,
            n.a.value('FirstName[1]','nvarchar(max)') FirstName,
            LEFT(n.a.value('LastName[1]','nvarchar(max)'), LEN(n.a.value('LastName[1]','nvarchar(max)'))
                                                        -1-LEN(n.a.value('FirstName[1]','nvarchar(max)'))) LastName,
            n.a.value('StreetLine1[1]','nvarchar(max)') StreetLine1,
            n.a.value('City[1]','nvarchar(max)') City,
            n.a.value('StateName[1]','nvarchar(max)') StateName,
            n.a.value('StateCode[1]','nvarchar(max)') StateCode,
            n.a.value('PostalCode[1]','nvarchar(max)') PostalCode,
            n.a.value('CountryName[1]','nvarchar(max)') CountryName,
            n.a.value('CountryCode[1]','nvarchar(max)') CountryCode,
            n.a.value('PhoneNumber[1]','nvarchar(max)') PhoneNumber,
            n.a.value('PhoneNumber2[1]','nvarchar(max)') PhoneNumber2,
            n.a.value('TaxScheduleID[1]','nvarchar(max)') TaxScheduleID,
            n.a.value('ShowAddress[1]','nvarchar(max)') ShowAddress
       from AddressBook.nodes('Address') n(a)
        for xml path('Address'), root('AddressBook'), type
      )
       from @t t
cross apply xmldata.nodes('AddressBook') ab(AddressBook);

     select *
       from @t;
于 2012-12-06T20:00:25.957 回答