How to escape underscore character in PATINDEX pattern argument?

Solution 1:

I've always done it with brackets: '%[_]%'

Solution 2:

To match two underscores, each must be bracketed

'%[__]%' -- matches single _ with anything after

'%[_][_]%' -- matches two consecutive _

Solution 3:

You can escape using the [ and ] characters like so:

PRINT PATINDEX('%[_]%', '37_21')