Joining Text
CONCAT joins texts. If any part is NULL, the whole result is NULL. CONCAT_WS (with separator) skips NULLs:
Result:
| label | with_null_email | skips_null |
|---|---|---|
| Asha Rao - Mumbai | Asha Rao - asha@example.com | Asha Rao | Mumbai | asha@example.com |
| Dina Cohen - Tel Aviv | NULL | Dina Cohen | Tel Aviv |
|| means OR. Always use CONCAT(). (PIPES_AS_CONCAT mode changes this, but do not rely on it.)
Length and Case
Result:
| name | characters | upper_name | lower_name |
|---|---|---|---|
| Asha Rao | 8 | ASHA RAO | asha rao |
| Ben Carter | 10 | BEN CARTER | ben carter |
| Chen Wei | 8 | CHEN WEI | chen wei |
Cutting Text
| Function | Example | Result |
|---|---|---|
LEFT(s, n) | LEFT('MySQL', 2) | My |
RIGHT(s, n) | RIGHT('MySQL', 3) | SQL |
SUBSTRING(s, start, len) | SUBSTRING('MySQL', 3, 3) | SQL |
SUBSTRING_INDEX(s, sep, n) | SUBSTRING_INDEX('a@b.com', '@', 1) | a |
Result:
| username | domain | initial | |
|---|---|---|---|
| asha@example.com | asha | example.com | A |
| ben@example.com | ben | example.com | B |
Positions start at 1, not 0. A negative count in SUBSTRING_INDEX counts from the right.
Searching Inside Text
Result:
| at_position | example_position | |
|---|---|---|
| asha@example.com | 5 | 6 |
LOCATE and INSTR return 0 when the text is not found.
Cleaning Text
Result:
| trimmed | no_left | no_right | no_stars | replaced |
|---|---|---|---|---|
| padded | left | right | star | a/b/c |
Padding and Repeating
Result:
| padded_id | stars | reversed |
|---|---|---|
| 00001 | *** | oaR ahsA |
Result:
| order_code |
|---|
| ORD-0001 |
| ORD-0010 |
Formatting
Result:
| thousands | rounded | price_text |
|---|---|---|
| 1,234,567.89 | 1 | Rs 899.00 |
FORMAT returns text, so you cannot do math on it afterwards. Prefer formatting numbers in your application and keep numeric columns numeric in the database.
Numeric Functions
| Function | Example | Result |
|---|---|---|
ROUND(x, d) | ROUND(2.567, 1) | 2.6 |
TRUNCATE(x, d) | TRUNCATE(2.567, 1) | 2.5 |
CEIL(x) or CEILING | CEIL(2.1) | 3 |
FLOOR(x) | FLOOR(2.9) | 2 |
ABS(x) | ABS(-5) | 5 |
MOD(a, b) or a % b | MOD(17, 5) | 2 |
POWER(a, b) | POWER(2, 10) | 1024 |
SQRT(x) | SQRT(81) | 9 |
GREATEST(a, b, ...), LEAST(...) | GREATEST(3, 9, 5) | 9 |
Result:
| price | with_tax | rounded_up | rounded_down | stock_mod_7 |
|---|---|---|---|---|
| 899.00 | 1060.82 | 899 | 899 | 1 |
| 3.50 | 4.13 | 4 | 3 | 3 |
ROUND on a .5 value rounds away from zero for exact numbers (DECIMAL) but uses the C library rule for floating point:
Result:
| exact_decimal | floating | negative | to_hundreds |
|---|---|---|---|
| 3 | 2.0 | -3 | 1200 |
Division in MySQL returns a decimal. DIV gives integer division and % the remainder:
Result:
| decimal_division | integer_division | remainder | divide_by_zero |
|---|---|---|---|
| 3.5000 | 3 | 1 | NULL |
SELECT, 1 / 0 returns NULL (with a warning). In an INSERT or UPDATE under strict mode, it is an error. NULLIF(x, 0) avoids both.
Putting It Together: Clean Labels
Result:
| tidy_name | code | email_domain |
|---|---|---|
| Asha rao | CUST-001 | example.com |
| Ben carter | CUST-002 | example.com |
| Chen wei | CUST-003 | example.com |
| Dina cohen | CUST-004 | no email |
| Eli brown | CUST-005 | example.com |
| Fatima khan | CUST-006 | example.com |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
CONCAT with a NULL column | Whole result is NULL | CONCAT_WS or COALESCE |
Using || for text | Acts as OR | CONCAT() |
LENGTH for characters | Counts bytes | CHAR_LENGTH |
| Counting positions from 0 | Off-by-one results | MySQL positions start at 1 |
Doing math on FORMAT() output | Text, not numbers | Format at the end or in the app |
Practice
- Show each customer's initials (first letter of each word of the name).
- Create order codes like
ORD-00007withLPAD. - Extract the domain of each email with
SUBSTRING_INDEX. - Show prices rounded to the nearest 10 (
ROUND(x, -1)).
Conclusion
Use CONCAT and CONCAT_WS (never ||), SUBSTRING, SUBSTRING_INDEX, LOCATE, TRIM and REPLACE for text, ROUND, CEIL, FLOOR, MOD and DIV for numbers, and remember MySQL positions start at 1.

