Monday, July 11, 2016

Hi, I need a small tip from experts,
I have a table like below with
ABC( id int,datecreated datetime, CC Varchar(20),Accountnumber(20), name varchar(10), OldRecord XML, Newrecord XML)
I have some thousands of records in ABC table, 
I have data something like below
Select * from ABC

 id datecreated        CC      Accountnumber        name                   OldRecord                                                            Newrecord
1   2016-07-11     null       #4%&*@))@1       NICK                 <row cc='1234' name='nick'  date='2016-07-11'>           null 

now, i would like to search data from Oldrecord column or new record column which are XML data type columns 
like below:
SELECT * from ABC
where Oldrecord like '%Nick%' 
or 
SELECT * from ABC
where Oldrecord like '%CC=1234%' 
i tried running the queries above and i got below error:
Argument data type xml is invalid for argument 1 of like function.

Please help me on how to search data in XML column data type 



Answer:
SELECT Top 10 * from  [dbo].[AuditLog] with(nolock)
where  cast(NewRecord as nvarchar(max)) like '%cc=1234%'

1 comment:

https://blog.sqlauthority.com/2009/06/27/sql-server-fix-error-17892-logon-failed-for-login-due-to-trigger-execution-changed-database-context...