String and Math Functions
Functions take a value and return a new one: a name in capitals, the first word of a sentence, a
price rounded to the nearest hundred. They work on each row, in SELECT, WHERE or ORDER BY.
Syntax
Order of execution: FROM → WHERE → SELECT → DISTINCT → ORDER BY → LIMIT
A function in SELECT runs once per row that WHERE kept. Its alias can be used in
ORDER BY, but not in WHERE, which runs earlier.
CodeSELECT function_name(column, argument, ...) AS alias FROM table_name;
| Function | Returns |
|---|---|
UPPER(s), LOWER(s), INITCAP(s) | capitals, small letters, First Letter Of Each Word |
LENGTH(s) | number of characters |
LEFT(s, n), RIGHT(s, n) | first / last n characters |
SUBSTRING(s FROM start FOR n) | n characters from position start (the first is 1) |
SPLIT_PART(s, separator, n) | the n-th piece of s cut at each separator |
REPLACE(s, from, to), TRIM(s) | swap text, remove spaces at both ends |
a || b, CONCAT(a, b, ...) | joined text |
ROUND(x, d), TRUNC(x, d) | x to d decimals, rounded / cut off |
CEIL(x), FLOOR(x) | next whole number up / down |
a % b | the remainder of a / b |
Examples
Changing case and length
Make text look the same everywhere, or measure it.
CodeSELECT UPPER(city), LOWER(city), INITCAP('new delhi') FROM customers; SELECT product_name, LENGTH(product_name) AS name_length FROM products;
Cutting text
Take part of a value: the start, the end, or a piece between separators.
CodeSELECT LEFT(product_name, 5), RIGHT(product_name, 3) FROM products; SELECT SUBSTRING(account_no FROM 4 FOR 3) FROM accounts; -- 3 characters from the 4th SELECT full_name, SPLIT_PART(full_name, ' ', 2) AS last_name FROM customers;
Replacing and trimming
Clean values before you show or compare them.
CodeSELECT REPLACE(product_name, ' - ', ': ') FROM products; SELECT TRIM(' Yoga Mat '); -- 'Yoga Mat'
Joining text
Build one value out of several, and watch out for NULLs.
Code-- || gives NULL if any part is NULL SELECT product_name || ' (' || rating || ' stars)' FROM products; -- CONCAT skips NULLs SELECT CONCAT(product_name, ' (', rating, ' stars)') FROM products;
Rounding
Round a number to show it, or round in one direction on purpose.
CodeSELECT price * 0.85 AS sale, ROUND(price * 0.85, 2) AS to_paise, ROUND(price * 0.85, -2) AS to_hundred, TRUNC(price * 0.85) AS cut_off, CEIL(price * 0.85) AS up, FLOOR(price * 0.85) AS down FROM products;
Integer division
A whole number divided by a whole number stays a whole number.
CodeSELECT 7 / 2, 7 % 2, 7 / 2.0; -- 3, 1, 3.5 SELECT stock, stock / 10 AS full_tens, stock % 10 AS left_over FROM products;
In other databases
Function names differ more than any other part of SQL. UPPER, LOWER, REPLACE, TRIM,
ROUND and FLOOR work everywhere; for the rest:
| PostgreSQL | MySQL | SQL Server | Oracle |
|---|---|---|---|
LENGTH(s) | CHAR_LENGTH(s) (LENGTH counts bytes) | LEN(s) | LENGTH(s) |
INITCAP(s) | none | none | INITCAP(s) |
LEFT(s, n), RIGHT(s, n) | the same | the same | SUBSTR(s, 1, n), SUBSTR(s, -n) |
SUBSTRING(s FROM a FOR n) | the same, or SUBSTRING(s, a, n) | SUBSTRING(s, a, n) | SUBSTR(s, a, n) |
SPLIT_PART(s, ' ', 2) | SUBSTRING_INDEX twice | no single function | REGEXP_SUBSTR(s, '[^ ]+', 1, 2) |
a || b | CONCAT(a, b) (|| means OR) | a + b or CONCAT(a, b) | a || b |
NULL in || or CONCAT | CONCAT gives NULL | + gives NULL, CONCAT skips it | both skip it |
TRUNC(x, d) | TRUNCATE(x, d) | ROUND(x, d, 1) | TRUNC(x, d) |
CEIL(x) | CEIL(x) | CEILING(x) | CEIL(x) |
a % b | a % b | a % b | MOD(a, b) |
7 / 2 is 3 | 3.5000 | 3 | 3.5 |
Remember
- In PostgreSQL,
||returns NULL when any part is NULL andCONCATtreats NULL as empty text. - In PostgreSQL and SQL Server, integer / integer drops the remainder:
7 / 2is 3. Make one side a decimal (2.0) to keep it. - Positions start at 1, not 0.
ROUND(x, -2)rounds to hundreds;CEILalways goes up andFLOORalways goes down.
Practice
- Display Names From Emails (Easy)
- Packing Into Boxes of 12 (Easy)
- Masked Phone Numbers (Easy)