How to escape underscore character in PATINDEX pattern argument?

后端 未结 3 906
南笙
南笙 2020-12-24 01:54

I\'ve found a solution for finding the position of an underscore with PATINDEX :

DECLARE @a VARCHAR(10)  
SET     @a = \'37_21\'

PRINT PATINDEX(\'%_%\', @a)         


        
相关标签:
3条回答
  • 2020-12-24 02:09

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

    0 讨论(0)
  • 2020-12-24 02:11

    To match two underscores, each must be bracketed

    '%[__]%' -- matches single _ with anything after
    
    '%[_][_]%' -- matches two consecutive _
    
    0 讨论(0)
  • 2020-12-24 02:11

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

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

    0 讨论(0)
提交回复
热议问题