String Functions: CONCAT, UPPER, LOWER, TRIM, REPLACE
Manipulate text: join strings with CONCAT, change case with UPPER/LOWER, clean spaces with TRIM and swap or remove characters with REPLACE.
Player names arrive from scorers, websites and Excel sheets — with extra spaces, random capitals and mixed formats. String functions let you clean and reshape this text inside the query. They do three jobs:
| Job | Functions | What they do |
|---|---|---|
| Manipulation | CONCAT, UPPER, LOWER, TRIM, REPLACE | change the text |
| Calculation | LEN | measure the text |
| Extraction | LEFT, RIGHT, SUBSTRING | pull out part of the text |
This topic covers manipulation; the next topic covers calculation and extraction.
Definition
A string function is a single-row function whose input is text (a string, e.g.
VARCHAR) and which returns a modified string or information about that string.
| Function | Definition |
|---|---|
CONCAT(s1, s2, ...) | Combines two or more strings into one string, in the order given. |
UPPER(s) | Converts every letter to uppercase. |
LOWER(s) | Converts every letter to lowercase. |
TRIM(s) | Removes leading and trailing spaces (spaces at the start and end). Spaces in the middle stay. |
REPLACE(s, old, new) | Replaces every occurrence of old inside s with new. If new is '', the text is removed. |
Syntax
sql
CONCAT(string1, string2 [, string3 ...])
UPPER(string)
LOWER(string)
TRIM(string) -- also: LTRIM(string), RTRIM(string)
REPLACE(string, old_value, new_value)CONCAT — combine strings
CONCAT('Virat', ' ', 'Kohli') → 'Virat Kohli'. The space ' ' is its own argument — without it you get 'ViratKohli'.
Our player_profiles table (the PDF's chapter-7 "players" table) has separate first_name and last_name columns:
sql
SELECT
CONCAT(first_name, ' ', last_name) AS full_name,
UPPER(team) AS team_u,
LEFT(last_name, 3) AS short
FROM player_profiles;| full_name | team_u | short |
|---|---|---|
| Virat Kohli | RCB | Koh |
| Rohit Sharma | MI | Sha |
| Jos Buttler | RR | But |
| MS Dhoni | CSK | Dho |
| David Warner | NULL | War |
| Jasprit Bumrah | MI | Bum |
How it works: each row is processed alone — the functions build three new columns per row. David Warner has no team (NULL), and UPPER(NULL) is still NULL: almost every function returns NULL when its input is NULL.
Note:
CONCATis the exception — it treats aNULLargument as an empty string.CONCAT(name, ' (', team, ')')gives'David Warner ()', not NULL.
UPPER & LOWER — consistent case
UPPER('Virat') → 'VIRAT', LOWER('VIRAT') → 'virat'. Applying the function to text that is already in that case changes nothing. They are perfect for making text consistent before comparing or grouping: 'rcb', 'Rcb' and 'RCB' all become 'RCB' after UPPER.
TRIM — remove extra spaces
'Dhoni', ' Dhoni', 'Dhoni ' and ' Dhoni ' all become 'Dhoni' after TRIM. Use LTRIM to clean only the left (leading) side and RTRIM for only the right (trailing) side.
sql
SELECT
CONCAT('[', TRIM(' Dhoni '), ']') AS trimmed,
CONCAT('[', LTRIM(' Dhoni '), ']') AS left_trim,
CONCAT('[', RTRIM(' Dhoni '), ']') AS right_trim;| trimmed | left_trim | right_trim |
|---|---|---|
| [Dhoni] | [Dhoni ] | [ Dhoni] |
The square brackets are only there so you can see the spaces.
REPLACE — swap or remove
sql
SELECT
REPLACE('2025-08-20', '-', '/') AS swapped,
REPLACE('2025-08-20', '-', '') AS removed;| swapped | removed |
|---|---|
| 2025/08/20 | 20250820 |
REPLACE is not only for replacing — with '' (nothing) as the new value it removes characters.
Example — build e-mail handles
sql
SELECT
name,
LOWER(REPLACE(name, ' ', '.')) AS handle,
CONCAT(LOWER(REPLACE(name, ' ', '.')), '@ipl.com') AS email
FROM players;| name | handle | |
|---|---|---|
| Virat Kohli | virat.kohli | virat.kohli@ipl.com |
| Rohit Sharma | rohit.sharma | rohit.sharma@ipl.com |
| Jos Buttler | jos.buttler | jos.buttler@ipl.com |
| MS Dhoni | ms.dhoni | ms.dhoni@ipl.com |
| David Warner | david.warner | david.warner@ipl.com |
| Jasprit Bumrah | jasprit.bumrah | jasprit.bumrah@ipl.com |
How it works: REPLACE swaps the space for a dot, LOWER makes it lowercase, and CONCAT glues on the domain — three nested functions, evaluated inside-out.
Common mistakes
- ❌
CONCAT(first_name, last_name)→'ViratKohli'. ✅ Add the separator:CONCAT(first_name, ' ', last_name). - ❌ Thinking
TRIMremoves spaces in the middle ('MS Dhoni'keeps both spaces). ✅ UseREPLACE(x, ' ', ' ')for inner spaces. - ❌ Comparing dirty text directly:
WHERE team = 'RCB'misses' rcb '. ✅WHERE UPPER(TRIM(team)) = 'RCB'.
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Join strings | CONCAT(a, b) or a + b | CONCAT(a, b) | CONCAT(a, b) or a || b |
NULL inside + / || | 'A' + NULL → NULL | — | 'A' || NULL → NULL |
Note: In SQL Server you can also write
first_name + ' ' + last_name, but if any part is NULL the whole result becomes NULL — another reason to preferCONCAT. The browser playground does not support+for strings, so useCONCATthere.
Interview tip
Q: What does CONCAT('Team: ', NULL) return, and how is that different from 'Team: ' + NULL?
CONCAT treats NULL as an empty string, so it returns 'Team: '. The + operator propagates NULL, so 'Team: ' + NULL returns NULL.
Key points to remember
- CONCAT joins strings in order; add ' ' yourself as a separator. It treats NULL arguments as empty strings.
- UPPER / LOWER convert every letter's case — ideal for consistent comparisons and grouping.
- TRIM removes leading and trailing spaces only; LTRIM cleans the left side, RTRIM the right side.
- REPLACE(value, old, new) swaps every occurrence of old; using '' as new removes the characters.
- Most functions return NULL when the input is NULL — UPPER(NULL) is NULL.
- In SQL Server, 'A' + NULL is NULL, which is why CONCAT is safer than + for joining columns.
Try it yourself
Full name, upper-case team and short code
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Clean messy sign-up data with TRIM and UPPER
This example runs a small setup step first — click to see it
CREATE TABLE raw_signups (id INT, player VARCHAR(30), team VARCHAR(10)); INSERT INTO raw_signups VALUES (1, ' Virat Kohli', 'rcb'), (2, 'Rohit Sharma ', 'Mi'), (3, ' MS Dhoni ', 'csk ');
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
REPLACE: swap and remove characters
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: build a scoreboard label
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.
