Example 1
Get the character count of each product name
'Keyboard' has 8 characters, 'Mouse' has 5, and 'Monitor' has 7. The results are identical across engines for these inputs because none of the values have trailing spaces that would cause LEN() and LENGTH() to differ.
CREATE TABLE products (id INT, name VARCHAR(50));
INSERT INTO
products (id, name)
VALUES
(1, 'Keyboard'),
(2, 'Mouse'),
(3, 'Monitor');SELECT
id,
name,
LENGTH (name) AS name_length
FROM
products
ORDER BY
id;| id | name | name_length |
|---|---|---|
| 1 | Keyboard | 8 |
| 2 | Mouse | 5 |
| 3 | Monitor | 7 |
CREATE TABLE products (id INT, name VARCHAR(50));
INSERT INTO
products (id, name)
VALUES
(1, 'Keyboard'),
(2, 'Mouse'),
(3, 'Monitor');SELECT
id,
name,
LEN (name) AS name_length
FROM
products
ORDER BY
id;| id | name | name_length |
|---|---|---|
| 1 | Keyboard | 8 |
| 2 | Mouse | 5 |
| 3 | Monitor | 7 |
SQL Server uses LEN() while other engines use LENGTH(). Results are identical for strings without trailing spaces.