user3290807
user3290807

Reputation: 391

ESCAPE SEQUENCE IN SQL SERVER

I have to do a where by clause on my table to find the exact string match in my table it works fine in the following scenario

declare @issuerName varchar(max)
set @issuerName = 'WIDEOPENWEST FINANCE, LLC'

select * 
from company 
where ParentName = @issuerName

but fails for the following query

declare @issuerName varchar(max)
set @issuerName = 'TOYS 'R' US, INC'

select * 
from company 
where ParentName = @issuerName

because of the single quotes.

Could someone let me know what it would be the best way to tackle it since single quotes can appear any where in the string? I tried adding single quotes around this variable but it doesn't work.

set @issuerName = ''TOYS 'R' US, INC''

Upvotes: 1

Views: 1774

Answers (1)

Yuck
Yuck

Reputation: 50855

You'd do this:

set @issuerName = 'TOYS ''R'' US, INC'

Putting the escaped ' within the string itself.

Further, if you need to change the escaped character as with a LIKE clause you may want to check out the ESCAPE keyword which lets you change the character used for the current query. Please see Using [] and ESCAPE clause in SQL Server LIKE query.

Upvotes: 5

Related Questions