Phillip Senn
Phillip Senn

Reputation: 47595

How to add a dash to pattern matching

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.

Upvotes: 10

Views: 10945

Answers (2)

Pondlife
Pondlife

Reputation: 16240

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.:

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

Upvotes: 8

davek
davek

Reputation: 22915

use

...ESCAPE '\'

e.g.

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

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

Upvotes: 16

Related Questions