Example 1
Find the position of @ in each email address
The @ in alice@example.com is at position 6 (a=1, l=2, i=3, c=4, e=5, @=6). In bob@company.net it is at position 4. In noreply@support.io it is at position 8. The key difference between engines is argument order: INSTR and STRPOS take (string, pattern) while SQL Server's CHARINDEX takes (pattern, string). A return value of 0 means the substring was not found.
CREATE TABLE contacts (id INT, email VARCHAR(100));
INSERT INTO
contacts (id, email)
VALUES
(1, 'alice@example.com'),
(2, 'bob@company.net'),
(3, 'noreply@support.io');SELECT
email,
INSTR (email, '@') AS at_position
FROM
contacts
ORDER BY
id;| at_position | |
|---|---|
| alice@example.com | 6 |
| bob@company.net | 4 |
| noreply@support.io | 8 |
CREATE TABLE contacts (id INT, email VARCHAR(100));
INSERT INTO
contacts (id, email)
VALUES
(1, 'alice@example.com'),
(2, 'bob@company.net'),
(3, 'noreply@support.io');SELECT
email,
CHARINDEX ('@', email) AS at_position
FROM
contacts
ORDER BY
id;| at_position | |
|---|---|
| alice@example.com | 6 |
| bob@company.net | 4 |
| noreply@support.io | 8 |
CREATE TABLE contacts (id INT, email VARCHAR(100));
INSERT INTO
contacts (id, email)
VALUES
(1, 'alice@example.com'),
(2, 'bob@company.net'),
(3, 'noreply@support.io');SELECT
email,
STRPOS (email, '@') AS at_position
FROM
contacts
ORDER BY
id;| at_position | |
|---|---|
| alice@example.com | 6 |
| bob@company.net | 4 |
| noreply@support.io | 8 |
MySQL/MariaDB/SQLite use INSTR(string, substr). PostgreSQL uses STRPOS(string, substr). SQL Server uses CHARINDEX(substr, string) — argument order is reversed. All return the same 1-based positions.