MySQL String and Number Functions You Will Use Every Day

MySQL 55 min min read Updated: Nov 27, 2026 Beginner
MySQL String and Number Functions You Will Use Every Day
Beginner Topic 2 of 3

Joining Text

CONCAT joins texts. If any part is NULL, the whole result is NULL. CONCAT_WS (with separator) skips NULLs:

sql SELECT CONCAT(name, ' - ', city) AS label, CONCAT(name, ' - ', email) AS with_null_email, CONCAT_WS(' | ', name, city, email) AS skips_null FROM customers WHERE id IN (1, 4) ORDER BY id;

Result:

labelwith_null_emailskips_null
Asha Rao - MumbaiAsha Rao - asha@example.comAsha Rao | Mumbai | asha@example.com
Dina Cohen - Tel AvivNULLDina Cohen | Tel Aviv
|| is not text joining in MySQL In the default mode || means OR. Always use CONCAT(). (PIPES_AS_CONCAT mode changes this, but do not rely on it.)

Length and Case

sql SELECT name, CHAR_LENGTH(name) AS characters, UPPER(name) AS upper_name, LOWER(name) AS lower_name FROM customers WHERE id <= 3 ORDER BY id;

Result:

namecharactersupper_namelower_name
Asha Rao8ASHA RAOasha rao
Ben Carter10BEN CARTERben carter
Chen Wei8CHEN WEIchen wei

Cutting Text

FunctionExampleResult
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
sql SELECT email, SUBSTRING_INDEX(email, '@', 1) AS username, SUBSTRING_INDEX(email, '@', -1) AS domain, LEFT(name, 1) AS initial FROM customers WHERE id IN (1, 2) ORDER BY id;

Result:

emailusernamedomaininitial
asha@example.comashaexample.comA
ben@example.combenexample.comB

Positions start at 1, not 0. A negative count in SUBSTRING_INDEX counts from the right.

Searching Inside Text

sql SELECT email, LOCATE('@', email) AS at_position, INSTR(email, 'example') AS example_position FROM customers WHERE id = 1;

Result:

emailat_positionexample_position
asha@example.com56

LOCATE and INSTR return 0 when the text is not found.

Cleaning Text

sql SELECT TRIM(' padded ') AS trimmed, LTRIM(' left') AS no_left, RTRIM('right ') AS no_right, TRIM(BOTH '*' FROM '**star**') AS no_stars, REPLACE('a-b-c', '-', '/') AS replaced;

Result:

trimmedno_leftno_rightno_starsreplaced
paddedleftrightstara/b/c

Padding and Repeating

sql SELECT LPAD(id, 5, '0') AS padded_id, REPEAT('*', 3) AS stars, REVERSE(name) AS reversed FROM customers WHERE id IN (1, 12) ORDER BY id;

Result:

padded_idstarsreversed
00001***oaR ahsA
sql SELECT CONCAT('ORD-', LPAD(id, 4, '0')) AS order_code FROM orders WHERE id IN (1, 10) ORDER BY id;

Result:

order_code
ORD-0001
ORD-0010

Formatting

sql SELECT FORMAT(1234567.891, 2) AS thousands, FORMAT(0.5, 0) AS rounded, CONCAT('Rs ', FORMAT(899, 2)) AS price_text;

Result:

thousandsroundedprice_text
1,234,567.891Rs 899.00
Format in the app 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

FunctionExampleResult
ROUND(x, d)ROUND(2.567, 1)2.6
TRUNCATE(x, d)TRUNCATE(2.567, 1)2.5
CEIL(x) or CEILINGCEIL(2.1)3
FLOOR(x)FLOOR(2.9)2
ABS(x)ABS(-5)5
MOD(a, b) or a % bMOD(17, 5)2
POWER(a, b)POWER(2, 10)1024
SQRT(x)SQRT(81)9
GREATEST(a, b, ...), LEAST(...)GREATEST(3, 9, 5)9
sql SELECT price, ROUND(price * 1.18, 2) AS with_tax, CEIL(price) AS rounded_up, FLOOR(price) AS rounded_down, MOD(stock, 7) AS stock_mod_7 FROM products WHERE id IN (1, 6) ORDER BY id;

Result:

pricewith_taxrounded_uprounded_downstock_mod_7
899.001060.828998991
3.504.13433

ROUND on a .5 value rounds away from zero for exact numbers (DECIMAL) but uses the C library rule for floating point:

sql SELECT ROUND(2.5) AS exact_decimal, ROUND(2.5E0) AS floating, ROUND(-2.5) AS negative, ROUND(1234.567, -2) AS to_hundreds;

Result:

exact_decimalfloatingnegativeto_hundreds
32.0-31200

Division in MySQL returns a decimal. DIV gives integer division and % the remainder:

sql SELECT 7 / 2 AS decimal_division, 7 DIV 2 AS integer_division, 7 % 2 AS remainder, 1 / 0 AS divide_by_zero;

Result:

decimal_divisioninteger_divisionremainderdivide_by_zero
3.500031NULL
Division by zero In a plain 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

sql SELECT CONCAT(UPPER(LEFT(name, 1)), LOWER(SUBSTRING(name, 2))) AS tidy_name, CONCAT('CUST-', LPAD(id, 3, '0')) AS code, COALESCE(SUBSTRING_INDEX(email, '@', -1), 'no email') AS email_domain FROM customers ORDER BY id;

Result:

tidy_namecodeemail_domain
Asha raoCUST-001example.com
Ben carterCUST-002example.com
Chen weiCUST-003example.com
Dina cohenCUST-004no email
Eli brownCUST-005example.com
Fatima khanCUST-006example.com

Common Beginner Mistakes

MistakeWhat happensFix
CONCAT with a NULL columnWhole result is NULLCONCAT_WS or COALESCE
Using || for textActs as ORCONCAT()
LENGTH for charactersCounts bytesCHAR_LENGTH
Counting positions from 0Off-by-one resultsMySQL positions start at 1
Doing math on FORMAT() outputText, not numbersFormat at the end or in the app

Practice

  1. Show each customer's initials (first letter of each word of the name).
  2. Create order codes like ORD-00007 with LPAD.
  3. Extract the domain of each email with SUBSTRING_INDEX.
  4. 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.

Quick Summary: CONCAT, SUBSTRING, LOCATE, TRIM, REPLACE, LPAD, FORMAT, ROUND, CEIL, FLOOR, MOD, DIV.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators