searching xml column

Hi

I have a table that contains a xml column 'propertiesXML'. I would like to select all rows in this column where one of the nodes in the xml document starts with something (for example 'S').

If the propertiesXML column contains 3 rows:

<properties>
<property name="name">Sally</property>
<property name="id">21003993</property>
</properties>

<properties>
<property name="name">Susan</property>
<property name="id">2243567</property>
</properties>

<properties>
<property name="name">Abel</property>
<property name="id">202435</property>
</properties>

I would like to select only the two rows where 'properties/property[starts-with(@name,"S")]'

Only the starts-with function does not seem to work in sql. Is there any other function i can use or another way i can do it

below is the querry

DECLARE @myName1 varchar(50)
DECLARE @myText1 varchar(50)

SELECT @myName1 = 'name'
SELECT @myText1 = 'S'

SELECT propertiesXML
FROM MyPerson
WHERE propertiesXML.exist('properties/property[@name=sql:variable("@myName1")and starts-with(text(), sql:variable("@myText1"))]') = 1



Answer this question

searching xml column

  • njm137

    Use substring() to get the prefix and use = to compare:

    SELECT popertiesXML
    FROM
    MyPerson
    WHERE propertiesXML.exist('properties/property[@name=sql:variable("@myName1")and substring(text()[1], 1,1)= sql:variable("@myText1")]') = 1

    Best regards

    Michael



  • searching xml column