Here are examples of returning rows that contain non-alphanumeric characters in SQL Server.
Non-alphanumeric characters include punctuation characters like !@#&()–[{}]:;',?/*
and symbols like `~$^+=<>“
, as well as whitespace characters like the space or tab characters.
Sample Data
Suppose we have the following table:
CREATE TABLE t1 (
c1 varchar(255) NULL
);
INSERT INTO t1 VALUES
('Music'),
('Live Music'),
('Café'),
('Café Del Mar'),
('100 Cafés'),
('[email protected]'),
('1 + 1'),
('()'),
('!@#&()–[{}]:;'',?/*'),
('`~$^+=<>“'),
('$1.50'),
('Player 456'),
('007'),
(null),
(''),
('é'),
('É'),
('é 123'),
('ø'),
('ø 123');
SELECT c1 FROM t1;
Result:
+----------------------+ | c1 | +----------------------+ | Music | | Live Music | | Café | | Café Del Mar | | 100 Cafés | | [email protected] | | 1 + 1 | | () | | !@#&()–[{}]:;',?/* | | `~$^+=<>“ | | $1.50 | | Player 456 | | 007 | | NULL | | | | é | | É | | é 123 | | ø | | ø 123 | +----------------------+
Example 1: Row Contains Only Non-Alphanumeric Characters
Here’s an example of code that returns rows that contain only non-alphanumeric characters:
SELECT c1 FROM t1
WHERE c1 NOT LIKE '%[a-zA-Z0-9]%';
Result:
+--------------------+ | c1 | |--------------------| | () | | !@#&()–[{}]:;',?/* | | `~$^+=<>“ | | | +--------------------+
Example 2: Row Contains Non-Alphanumeric Characters
Here’s an example of code that returns rows that contain non-alphanumeric characters, but could also contain alphanumeric characters:
SELECT c1 FROM t1
WHERE c1 LIKE '%[^a-zA-Z0-9]%';
Result:
+--------------------+ | c1 | |--------------------| | Live Music | | Café Del Mar | | 100 Cafés | | [email protected] | | 1 + 1 | | () | | !@#&()–[{}]:;',?/* | | `~$^+=<>“ | | $1.50 | | Player 456 | | é 123 | | ø 123 | +--------------------+
To exclude spaces:
SELECT c1 FROM t1
WHERE c1 LIKE '%[^a-zA-Z0-9 ]%';
Result:
+--------------------+ | c1 | |--------------------| | [email protected] | | 1 + 1 | | () | | !@#&()–[{}]:;',?/* | | `~$^+=<>“ | | $1.50 | +--------------------+