String Functions: LEN, LEFT, RIGHT, SUBSTRING
Measure text with LEN and extract parts of it with LEFT, RIGHT and SUBSTRING (with CHARINDEX for dynamic positions).
Need the first letter of every player for initials? The last 3 characters of a code? The surname hidden inside a full name? These are calculation (LEN) and extraction (LEFT, RIGHT, SUBSTRING) jobs.
Definition
| Function | Definition |
|---|---|
LEN(value) | Returns the number of characters in a value as an INT — letters, digits, spaces and symbols all count. Numbers are converted to text first, so LEN(750) = 3. Trailing spaces are not counted in SQL Server. |
LEFT(value, n) | Returns the first n characters from the start (left side) of the string. |
RIGHT(value, n) | Returns the last n characters from the end (right side) of the string. |
SUBSTRING(value, start, length) | Returns length characters, starting at position start. Positions start at 1 (not 0). |
Character positions: In
'Virat Kohli', V is position 1, the space is position 6 and K is position 7. SQL counts from 1, unlike Python which counts from 0.
| V | i | r | a | t | ␣ | K | o | h | l | i |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Syntax
sql
LEN(value)
LEFT(value, number_of_characters)
RIGHT(value, number_of_characters)
SUBSTRING(value, start_position, length)Example — the basics on one string
sql
SELECT
LEN('Virat') AS len_virat,
LEN('MS Dhoni') AS len_dhoni,
LEN(750) AS len_number,
LEN('2025-08-20') AS len_date,
LEFT('Virat Kohli', 2) AS left_2,
RIGHT('Virat Kohli', 3) AS right_3,
SUBSTRING('Virat Kohli', 7, 5) AS sub_7_5,
SUBSTRING('Virat Kohli', 1, 5) AS sub_1_5;| len_virat | len_dhoni | len_number | len_date | left_2 | right_3 | sub_7_5 | sub_1_5 |
|---|---|---|---|---|---|---|---|
| 5 | 8 | 3 | 10 | Vi | hli | Kohli | Virat |
How it works: 'MS Dhoni' is 8 because the space counts. 750 becomes the text '750' (3 characters). SUBSTRING('Virat Kohli', 7, 5) starts at K (position 7) and takes 5 characters → 'Kohli'.
Example — length, initial and last 3 letters
sql
SELECT
name,
LEN(name) AS name_length,
LEFT(name, 1) AS initial,
RIGHT(name, 3) AS last3
FROM players
ORDER BY LEN(name) DESC, name;| name | name_length | initial | last3 |
|---|---|---|---|
| Jasprit Bumrah | 14 | J | rah |
| David Warner | 12 | D | ner |
| Rohit Sharma | 12 | R | rma |
| Jos Buttler | 11 | J | ler |
| Virat Kohli | 11 | V | hli |
| MS Dhoni | 8 | M | oni |
Functions work in ORDER BY too — here we sort by the length of the name.
Example — dynamic extraction with CHARINDEX
Fixed positions break when names have different lengths. CHARINDEX(search, text) returns the position of the first match (0 if not found), so we can find the space and cut around it:
sql
SELECT
name,
CHARINDEX(' ', name) AS space_at,
LEFT(name, CHARINDEX(' ', name) - 1) AS first_part,
SUBSTRING(name, CHARINDEX(' ', name) + 1, LEN(name)) AS last_part
FROM players;| name | space_at | first_part | last_part |
|---|---|---|---|
| Virat Kohli | 6 | Virat | Kohli |
| Rohit Sharma | 6 | Rohit | Sharma |
| Jos Buttler | 4 | Jos | Buttler |
| MS Dhoni | 3 | MS | Dhoni |
| David Warner | 6 | David | Warner |
| Jasprit Bumrah | 8 | Jasprit | Bumrah |
How it works: the first name is everything before the space (space_at - 1 characters). The last name starts after the space (space_at + 1). Using LEN(name) as the length is a common trick meaning "take everything until the end" — SUBSTRING simply stops when the string runs out.
Common mistakes
- ❌
SUBSTRING(name, 0, 5)thinking positions start at 0. ✅ SQL positions start at 1. - ❌ Forgetting the space counts:
LEN('MS Dhoni')is 8, not 7. - ❌ Expecting
LEN('Dhoni ')to be 8. ✅ SQL Server'sLENignores trailing spaces → 5 (leading spaces are counted). UseDATALENGTHto count bytes including trailing spaces. - ❌ Hard-coding
SUBSTRING(name, 7, 5)for every row. ✅ Combine withCHARINDEX/LENfor a dynamic position and length.
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Length | LEN(s) | LENGTH(s) / CHAR_LENGTH(s) | LENGTH(s) |
| Substring | SUBSTRING(s, start, len) (len required) | SUBSTRING(s, start, len) | SUBSTRING(s, start, len) / SUBSTRING(s FROM start FOR len) |
| Find position | CHARINDEX(find, s) | LOCATE(find, s) / INSTR(s, find) | POSITION(find IN s) |
Interview tip
Q: Extract the first name from a full name stored in one column.
LEFT(name, CHARINDEX(' ', name) - 1). Mention the edge case: if a name has no space, CHARINDEX returns 0 and LEFT(name, -1) raises an error in SQL Server — guard it with CASE WHEN CHARINDEX(' ', name) > 0 THEN … ELSE name END.
Key points to remember
- LEN counts characters (letters, digits, spaces, symbols) and returns an INT; SQL Server ignores trailing spaces.
- Numbers are counted as text: LEN(750) = 3.
- LEFT(value, n) returns the first n characters; RIGHT(value, n) returns the last n.
- SUBSTRING(value, start, length) extracts from the middle; positions start at 1, not 0.
- CHARINDEX(find, value) returns the position of a character so LEFT/SUBSTRING can cut dynamically.
- Using LEN(value) as SUBSTRING's length is a simple way to say 'until the end of the string'.
Try it yourself
LEN, LEFT, RIGHT and SUBSTRING on one string
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Split full names into first and last name
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Initials-style short names
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: longest names first
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
