I have an interesting behavior that I would like to better understand so I don't get hung up misusing it by accident.
The following is an example of escaping a '%' in a WHERE clause:
select * from #z where b like '%e%' ESCAPE 'e'
In this table with these values:
create table #z (a int, b varchar(10))
insert into #z values (1, 'e25%')
insert into #z values (2, '25')
insert into #z values (3, '18%')
insert into #z values (4, 'cab')
insert into #z values (5, '2%')
selects the three rows with the % at the end.
However, if I replace like '%e%'
with like 'ee%'
it selects only the first row. Thus it appears that double-escaping the % is like a double-negative, and undoes the escape, thus making the % back into the wildcard again. Am I understanding this correctly? Or is there something else going on here?