1

I have an XML Query like this:

<ChangeSet xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   <Change DateTime="2011-12-02T09:01:58.3615661-08:00" UserId="3123">
      <Table ChangeType="Insert" Name="EVNT_LN_AFF">
         <Keys>
            <Key FieldName="DIR_CD" Value="NB" />
            <Key FieldName="LN_ID" Value="A" />
            <Key FieldName="EVNT_ID" Value="10T000289" />
         </Keys>
         <ChangedFields>
            <Field FieldName="DIR_CD" Previous="" Current="NB" />
            <Field FieldName="LN_ID" Previous="" Current="A" />
            <Field FieldName="EVNT_ID" Previous="" Current="10T000289" />
            <Field FieldName="UD_DTTM" Previous="" Current="12/2/2011 9:01:59 AM" />
            <Field FieldName="UD_USER_ID" Previous="" Current="3123" />
         </ChangedFields>
      </Table>

(The query goes on)

Now I want to use a statement like this:

SELECT TOP 1000 [CHG_LOG_ID]
   , [EVNT_ID]
   , [DATA_XML_TXT]
   , [UD_DTTM]
FROM [MY_PROJ].[dbo].[EVNT_CHG_LOG]
WHERE DATA_XML_TXT.value('(/ChangeSet/Change/Table/ChangedFields/UD_USER_ID)[0]','varchar(50)') like '%3123%'

But when I execute the query, I don't get any results.

1
  • From your sample XML it appears you can have multiple Change elements in the ChangeSet element. Or even multiple Table elements. Is this correct? Commented Dec 6, 2011 at 8:47

1 Answer 1

2

I tested the following XQuery, and it should give you what you need:

SELECT TOP 1000 [CHG_LOG_ID]
   , [EVNT_ID]
   , [DATA_XML_TXT]
   , [UD_DTTM]
FROM [MY_PROJ].[dbo].[EVNT_CHG_LOG]
WHERE DATA_XML_TXT.value('(/ChangeSet/Change/Table/ChangedFields/Field[@FieldName="UD_USER_ID"]/@Current)[1]','varchar(50)') like '%3123%'

Note: Indexing for XQuery starts at 1 instead of 0

Sign up to request clarification or add additional context in comments.

1 Comment

That worked. I was not sure how to use "@Current" in my code. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.