Using LIKE with apostrophes

All we need is an easy explanation of the problem, so here it is.

I have been running a server side trace in order to find out how a specific stored procedure is called within my database.

I am running the following query in order to get data from the trace file generated:

select TextData,
db_name(databaseid)
from fn_trace_gettable( 'R:\Profiler\udpProductTaxRateGet4.trc', default) fn
join sys.trace_events te
 on fn.EventClass = te.trace_event_id
where --
1=1
--and EventClass = 42
and textdata like '%udpProductBulletPointSelectByTier1NoteTypeCode%'
--and db_name(databaseid) = 'US16AUTPProduct'
order by starttime desc

But as you can see on the picture below, there are too many rows like this one:

declare @p5 int  set @p5=86400 exec dbo.udpProductBulletPointSelectByTier1NoteTypeCode @Tier1=N'AR763',@LanguageID=2,@SeasonItemID=N'16AUT',@ListNoteTypeCode=N'',@[email protected] output  select @p5

enter image description here

more specifically the procedure was called with an empty parameter value for @ListNoveTypeCode

@ListNoteTypeCode=N”

How can I filter out all those rows where @ListNoteTypeCode=N”??

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The single quote character has no special significance to LIKE

You need to double them up to escape them inside any string literal.

So the two consecutive single quotes in the string you are trying not to match become four as below.

And textdata not like '%@ListNoteTypeCode=N''''%'

Method 2

From the image, it looks like the BulletPoint is what you need to look for as well. Change the Like statement to
like ‘%udpProductBulletPointSelectByTier1NoteTypeCode%BulletPoint%’

This should reduce the results.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply