Aral Code

Working with Data Lesson 7 of 13

MySQL String and Number Functions

Use MySQL functions to format and clean data — CONCAT, UPPER, LOWER, LENGTH, SUBSTRING, LEFT, REPLACE, TRIM, LPAD, ROUND, FLOOR, CEIL, MOD, FORMAT, IF, IFNULL and CASE.

MySQL has hundreds of built-in functions. They let you clean and format data right in your query, so your app receives it ready to display. Live examples use SQLite, which shares many of these; MySQL-only functions are shown with their results.

String functions

SQL Formatting names
SELECT CONCAT(last_name, ', ', first_name) AS formal_name,
       UPPER(last_name) AS upper_last,
       LOWER(first_name) AS lower_first,
       LENGTH(first_name) AS letters,
       SUBSTR(first_name, 1, 1) || SUBSTR(last_name, 1, 1) AS initials
FROM students;
FunctionExampleResult
CONCAT(a, b, ...)CONCAT('Juan', ' ', 'Cruz')Juan Cruz
CONCAT_WS(sep, ...)CONCAT_WS('-', '2026', '12', '25')2026-12-25
UPPER, LOWERUPPER('kape')KAPE
CHAR_LENGTH(s)CHAR_LENGTH('Niño')4
LENGTH(s)LENGTH('Niño')5 (bytes!)
SUBSTRING(s, pos, len)SUBSTRING('Pilipinas', 1, 4)Pili
LEFT, RIGHTRIGHT('09171234567', 4)4567
REPLACE(s, a, b)REPLACE('09-17', '-', '')0917
TRIM(s)TRIM(' Ana ')Ana
LPAD(s, n, pad)LPAD(42, 5, '0')00042
LOCATE(sub, s)LOCATE('pin', 'Pilipinas')5

Masking a phone number

SELECT name, CONCAT(LEFT(phone, 4), '***', RIGHT(phone, 4)) AS masked
FROM customers
WHERE phone IS NOT NULL;

Output

+------------------+-------------+
| name             | masked      |
+------------------+-------------+
| Maria Santos     | 0917***4567 |
| Jose Reyes       | 0928***4567 |
| Pedro Bautista   | 0939***4567 |
| ...              | ...         |
+------------------+-------------+

Number functions

SQL Rounding and remainders
SELECT ROUND(88.456, 2) AS r2,
       ROUND(89.5) AS r0,
       ABS(-15) AS absolute,
       17 % 5 AS remainder,
       CAST(17 / 5 AS INTEGER) AS whole_part;

MySQL also has FLOOR, CEIL, MOD(17, 5), POWER(2, 10), SQRT(144), TRUNCATE(9.99, 1) (9.9, no rounding) and 17 DIV 5 (integer division, 3).

FORMAT for display

SELECT FORMAT(1234567.891, 2) AS pesos;

Output

+--------------+
| pesos        |
+--------------+
| 1,234,567.89 |
+--------------+

FORMAT returns text with commas, ready to show. Don't use it on values you'll do more math with.

Conditional functions

SQL CASE and IFNULL (COALESCE)
SELECT name, stock,
  CASE
    WHEN stock = 0 THEN 'Out of stock'
    WHEN stock < 30 THEN 'Low'
    ELSE 'OK'
  END AS status
FROM products;

SELECT name, COALESCE(phone, 'no phone') AS contact FROM customers;

MySQL also has the shorter IF(condition, then, else), like IF(stock = 0, 'Sold out', 'Available'), and IFNULL(phone, 'no phone'). CASE and COALESCE work in every database, so they're good habits.

Grouped text: GROUP_CONCAT

SELECT category, GROUP_CONCAT(name ORDER BY name SEPARATOR ', ') AS items
FROM products
GROUP BY category;

Output

+---------------+--------------------------------------------+
| category      | items                                      |
+---------------+--------------------------------------------+
| Bread         | Pandesal (10 pcs)                          |
| Canned Goods  | Canned Sardines, Corned Beef               |
| Cooking       | Cooking Oil (sachet), Soy Sauce            |
| Drinks        | Bottled Water, Coffee 3-in-1, Softdrink 1.5L |
| ...           | ...                                        |
+---------------+--------------------------------------------+

SQLite's equivalent is group_concat(name, ', '). It's great for showing, for example, all subjects a student failed in one cell.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which function counts characters correctly for "Niño"?
2. What does LPAD(7, 3, '0') return?
3. What does IFNULL(phone, 'none') return when phone is NULL?
4. What does GROUP_CONCAT do?

By Sir Reyel · Last updated 2026-12-04