2017-01-09 9 views
0

私は、列の1つがXMLコードを1行に集約したテーブルを持っています。 XMLであるため、charIndexを実行することはできません。行内には多くの '<' '>があります。キーワード検索の左側の列に大きな文字列からSQLを抽出する文字列

私は列の文字列検索から2文字のコードを抽出する方法が必要です。

EX)欄が 'XMLMessage' と呼ばれ、含まれています

<p1:UniversalInterchange xmlns:p1="http://www.cargowise.com/Schemas/Native"> 
    <Header xmlns=""> 
    <SenderID/> 
    <RecipientID/> 
    </Header> 
    <Body xmlns=""> 
    <UniversalEvent> 
     <Event> 
     <EventType>DEP</EventType> 
     <EventTime>2016-02-04T13:37:00</EventTime> 
     <ContextCollection> 
      <Context> 
      <Type>MAWBNumber</Type> 
      <Value>057-23154670</Value> 
      </Context> 
      <Context> 
      <Type>MAWBOriginIATAAirportCode</Type> 
      <Value>IAD</Value> 
      </Context> 
      <Context> 
      <Type>MAWBDestinationIATAAirportCode</Type> 
      <Value>TUN</Value> 
      </Context> 
      <Context> 
      <Type>MAWBNumberOfPieces</Type> 
      <Value>1</Value> 
      </Context> 
      <Context> 
      <Type>OtherServiceInfo</Type> 
      <Value> 
       PRD-XPS 
      </Value> 
      </Context> 
      <Context> 
      <Type>SourceEventCode</Type> 
      <Value>DEP</Value> 
      </Context> 
      <Context> 
      <Type>NumberOfPieces</Type> 
      <Value>1</Value> 
      </Context> 
      <Context> 
      <Type>WeightOfGoods</Type> 
      <Value>2KG</Value> 
      </Context> 
      <Context> 
      <Type>IATACarrierCode</Type> 
      <Value>AF</Value> 
      </Context> 
      <Context> 
      <Type>FlightNumber</Type> 
      <Value>1184</Value> 
      </Context> 
      <Context> 
      <Type>FlightDate</Type> 
      <Value>04-FEB-2016</Value> 
      </Context> 
      <Context> 
      <Type>OriginIATAAirportCode</Type> 
      <Value>CDG</Value> 
      </Context> 
      <Context> 
      <Type>DestinationIATAAirportCode</Type> 
      <Value>TUN</Value> 
      </Context> 
      <Context> 
      <Type>TimeOfDeparture</Type> 
      <Value>2016-02-04T13:37:00</Value> 
      </Context> 
      <Context> 
      <Type>TimeOfArrival</Type> 
      <Value>2016-02-04T15:40:00</Value> 
      </Context> 
     </ContextCollection> 
     <AdditionalFieldsToUpdateCollection> 
      <AdditionalFieldsToUpdate> 
      <Type>JobConsolTransport.JW_ATD</Type> 
      <Value>2016-02-04T13:37:00</Value> 
      </AdditionalFieldsToUpdate> 
     </AdditionalFieldsToUpdateCollection> 
     </Event> 
    </UniversalEvent> 
    </Body> 
</p1:UniversalInterchange> 

私は 'IATACarrierCode' 検索後に 'AF' を見つける必要があります。

'IATACarrierCode'の後に 'AF'を見つけるためにselect文が必要です。それは常に2文字の後に続きます。私はあなたの質問を理解していれば

+0

使用するXQuery httpsで動作します://それはXML列であると仮定すると、

msdn.microsoft.com/en-us/library/ms191474.aspx –

答えて

0
SELECT distinct substring(EI_BodyNText, CHARINDEX('IATACarrierCode</Type><Value>', EI_BodyNText)+29, 2) from dbo.EDIInterchange 

これはCHARINDEXとサブ

0

が正しくこれは、私は

 create table ##aw 
     (
      strr varchar(max) 
     ) 

     insert ##aw values('<p1:UniversalInterchange xmlns:p1="http://www.cargowise.com/Schemas/Native"><Header xmlns=""><SenderID/><RecipientID/></Header><Body xmlns=""><UniversalEvent><Event><EventType>DEP</EventType><EventTime>2016-02-04T13:37:00</EventTime><ContextCollection><Context><Type>MAWBNumber</Type><Value>057-23154670</Value></Context><Context><Type>MAWBOriginIATAAirportCode</Type><Value>IAD</Value></Context><Context><Type>MAWBDestinationIATAAirportCode</Type><Value>TUN</Value></Context><Context><Type>MAWBNumberOfPieces</Type><Value>1</Value></Context><Context><Type>OtherServiceInfo</Type><Value>PRD-XPS 
     </Value></Context><Context><Type>SourceEventCode</Type><Value>DEP</Value></Context><Context><Type>NumberOfPieces</Type><Value>1</Value></Context><Context><Type>WeightOfGoods</Type><Value>2KG</Value></Context><Context><Type>IATACarrierCode</Type><Value>AF</Value></Context><Context><Type>FlightNumber</Type><Value>1184</Value></Context><Context><Type>FlightDate</Type><Value>04-FEB-2016</Value></Context><Context><Type>OriginIATAAirportCode</Type><Value>CDG</Value></Context><Context><Type>DestinationIATAAirportCode</Type><Value>TUN</Value></Context><Context><Type>TimeOfDeparture</Type><Value>2016-02-04T13:37:00</Value></Context><Context><Type>TimeOfArrival</Type><Value>2016-02-04T15:40:00</Value></Context></ContextCollection><AdditionalFieldsToUpdateCollection><AdditionalFieldsToUpdate><Type>JobConsolTransport.JW_ATD</Type><Value>2016-02-04T13:37:00</Value></AdditionalFieldsToUpdate></AdditionalFieldsToUpdateCollection></Event></UniversalEvent></Body></p1:UniversalInterchange>') 

     select substring(strr, charindex('IATACarrierCode</Type><Value>', strr) + len('IATACarrierCode</Type><Value>'), 2) from ##aw 
+0

ロジックは正しいですが、多くの行と文字列があります静的ではありません。理想的には、 'String'の代わりにColumnを使用する必要があります – VladimirY

0

SQL ServerがXMLをネイティブにサポートしている、文字列として以外のフィールドの内容を選択する理由はありませんでした方法です。

SELECT c.value('(../Value)[1]', 'varchar(30)') 
FROM table 
CROSS APPLY table.xmlfield.nodes('(//Context/Type[text()="IATACarrierCode"])[1]') as t(c) 

それともそれは単なる文字列だ場合::

DECLARE @val XML = '<p1:UniversalInterchange...blah blah blah' 
SELECT c.value('(../Value)[1]', 'varchar(30)') FROM @val.nodes('(//Context/Type[text()="IATACarrierCode"])[1]') as t(c) 
関連する問題