如何添加破折号以进行模式匹配

时间:2022-07-07 16:50:47

This works:

select Name
from Table
WHERE Name like '%[^0-9A-Za-z]%'

But now I need to add the dash character to the criteria as well.

但是现在我需要将短划线字符添加到标准中。

2 个解决方案

#1


8  

use

...ESCAPE '\'

e.g.

WHERE Name like '%[^0-9A-Za-z\-]%' ESCAPE '\'

to have the final "-" treated as a literal.

将最终的“ - ”视为文字。

#2


4  

Unless it's part of a range the hyphen is not a special character in LIKE patterns, so you can just add it to your pattern, e.g.:

除非它是一个范围的一部分,否则连字符不是LIKE模式中的特殊字符,所以你可以将它添加到你的模式中,例如:

select 
        [char]
from
    (
    select 'a' as 'char' union 
    select '-' union 
    select '$' union
    select '7'
    ) dt
where 
    [char] like '%[^A-Za-z0-9-]%'

#1


8  

use

...ESCAPE '\'

e.g.

WHERE Name like '%[^0-9A-Za-z\-]%' ESCAPE '\'

to have the final "-" treated as a literal.

将最终的“ - ”视为文字。

#2


4  

Unless it's part of a range the hyphen is not a special character in LIKE patterns, so you can just add it to your pattern, e.g.:

除非它是一个范围的一部分,否则连字符不是LIKE模式中的特殊字符,所以你可以将它添加到你的模式中,例如:

select 
        [char]
from
    (
    select 'a' as 'char' union 
    select '-' union 
    select '$' union
    select '7'
    ) dt
where 
    [char] like '%[^A-Za-z0-9-]%'