MySQL String Functions

MySQL-specific string functions

CONCAT_WS

SELECT CONCAT_WS('-, first_name, last_name) FROM users; # concat with separator

FIELD

SELECT * FROM users
ORDER BY FIELD(status, 'active', 'pending', 'inactive'); # custom order

FORMAT

SELECT FORMAT(price, 2) FROM products; # format number with decimals

CHAR_LENGTH

SELECT CHAR_LENGTH(name) FROM users; # character length (UTF-8 aware)

LOCATE

SELECT LOCATE('@', email) FROM users; # find position of substring

REPEAT

SELECT REPEAT('*', 5); # repeat string