Use a LIKE statement on SQL Server XML Datatype

前端 未结 4 1204
面向向阳花
面向向阳花 2020-12-30 18:21

If you have a varchar field you can easily do SELECT * FROM TABLE WHERE ColumnA LIKE \'%Test%\' to see if that column contains a certain string.

How do

相关标签:
4条回答
  • 2020-12-30 18:39

    You should be able to do this quite easily:

    SELECT * 
    FROM WebPageContent 
    WHERE data.value('(/PageContent/Text)[1]', 'varchar(100)') LIKE 'XYZ%'
    

    The .value method gives you the actual value, and you can define that to be returned as a VARCHAR(), which you can then check with a LIKE statement.

    Mind you, this isn't going to be awfully fast. So if you have certain fields in your XML that you need to inspect a lot, you could:

    • create a stored function which gets the XML and returns the value you're looking for as a VARCHAR()
    • define a new computed field on your table which calls this function, and make it a PERSISTED column

    With this, you'd basically "extract" a certain portion of the XML into a computed field, make it persisted, and then you can search very efficiently on it (heck: you can even INDEX that field!).

    Marc

    0 讨论(0)
  • 2020-12-30 18:48

    Yet another option is to cast the XML as nvarchar, and then search for the given string as if the XML vas a nvarchar field.

    SELECT * 
    FROM Table
    WHERE CAST(Column as nvarchar(max)) LIKE '%TEST%'
    

    I love this solution as it is clean, easy to remember, hard to mess up, and can be used as a part of a where clause.

    EDIT: As Cliff mentions it, you could use:

    ...nvarchar if there's characters that don't convert to varchar

    0 讨论(0)
  • 2020-12-30 18:55

    This is what I am going to use based on marc_s answer:

    SELECT 
    SUBSTRING(DATA.value('(/PAGECONTENT/TEXT)[1]', 'VARCHAR(100)'),PATINDEX('%NORTH%',DATA.value('(/PAGECONTENT/TEXT)[1]', 'VARCHAR(100)')) - 20,999)
    
    FROM WEBPAGECONTENT 
    WHERE COALESCE(PATINDEX('%NORTH%',DATA.value('(/PAGECONTENT/TEXT)[1]', 'VARCHAR(100)')),0) > 0
    

    Return a substring on the search where the search criteria exists

    0 讨论(0)
  • 2020-12-30 19:01

    Another option is to search the XML as a string by converting it to a string and then using LIKE. However as a computed column can't be part of a WHERE clause you need to wrap it in another SELECT like this:

    SELECT * FROM
        (SELECT *, CONVERT(varchar(MAX), [COLUMNA]) as [XMLDataString] FROM TABLE) x
    WHERE [XMLDataString] like '%Test%'
    
    0 讨论(0)
提交回复
热议问题