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.
The * is the zero-or-more repetition specifier. Now you should understand why this pattern does not match a hyphen: it matches zero-or-more of characters that is either a whitespace or a word character. If you want to match a hyphen, then you can include it into the character class.
LIKE clause is used to perform the pattern matching task in SQL. A WHERE clause is generally preceded by a LIKE clause in an SQL query.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.
use
...ESCAPE '\'
e.g.
WHERE Name like '%[^0-9A-Za-z\-]%' ESCAPE '\'
to have the final "-" treated as a literal.
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-]%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With