Use a LIKE statement on SQL Server XML Datatype

前端 未结 4 1203
面向向阳花
面向向阳花 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: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

提交回复
热议问题