Chapter 14: SQL Functions · Lesson 180 of 294
String Functions
String Functions
Scalar Functions vs Aggregates
From here on, this chapter shifts from aggregate functions (which collapse many rows into one value) to scalar functions — functions applied to a single value, once per row, that produce a transformed value per row. String functions manipulate text data.
CONCAT — Joining Strings Together
sqlSELECT CONCAT(customer_name, ' - ', status) AS summary FROM orders;
Result (for the first two rows):
summary
------------------
Alice - completed
Bob - pendingDialect variance: CONCAT(a, b, c) (the function form) works in MySQL, PostgreSQL, SQL Server (2012+), and Oracle. Some dialects — notably PostgreSQL, Oracle, and SQLite — also support (or exclusively prefer historically) the || concatenation operator:
sqlSELECT customer_name || ' - ' || status AS summary FROM orders; -- PostgreSQL / Oracle / SQLite
SQL Server historically used + for string concatenation instead of ||.
UPPER / LOWER — Case Conversion
sqlSELECT UPPER(customer_name) FROM orders WHERE id = 1; -- 'ALICE' SELECT LOWER(status) FROM orders WHERE id = 1; -- 'completed'
LENGTH — String Length
sqlSELECT LENGTH(customer_name) FROM orders WHERE customer_name = 'Charlie'; -- 7
Dialect variance: LENGTH() is standard/PostgreSQL/MySQL/SQLite; SQL Server uses LEN() instead.
SUBSTRING — Extracting Part of a String
sqlSELECT SUBSTRING(customer_name, 1, 3) FROM orders WHERE customer_name = 'Charlie'; -- 'Cha' (3 characters starting at position 1)
Signature: SUBSTRING(string, start_position, length). SQL Server also supports SUBSTRING() with this same signature; MySQL additionally accepts SUBSTR() as a common alias.
TRIM — Removing Leading/Trailing Whitespace
sqlSELECT TRIM(' Alice ') AS cleaned; -- 'Alice'
TRIM removes leading and trailing spaces by default; most dialects also support LTRIM()/RTRIM() for one-sided trimming, and an optional character argument (TRIM('x' FROM 'xxAlicexx') → 'Alice') to strip characters other than spaces.
REPLACE — Substituting Text
sqlSELECT REPLACE(status, 'completed', 'DONE') FROM orders WHERE id = 1; -- 'DONE'
REPLACE(string, search_value, replacement) swaps every occurrence of search_value with replacement.
Combining String Functions
sqlSELECT UPPER(SUBSTRING(customer_name, 1, 1)) AS initial FROM orders; -- 'A', 'B', 'A', 'C', 'B' (first initial, capitalized)
Edge Cases
CONCAT()with aNULLargument: in MySQL,CONCAT('a', NULL, 'b')returnsNULLfor the whole expression (any NULL "poisons" the result) — but PostgreSQL'sCONCAT()function specifically treats NULL as an empty string, while its||operator returns NULL if either side is NULL. Always test your specific engine's NULL behavior.SUBSTRINGwith an out-of-range start position or length returns an empty string or truncates gracefully rather than erroring, in most dialects.LENGTH('')returns0;LENGTH(NULL)returnsNULL.
Key Takeaways / Interview Q&A
Q: What's the difference between CONCAT() and ||?
Ans:CONCAT() is a function available across most dialects; || is an operator used in PostgreSQL, Oracle, and SQLite. SQL Server historically used + instead.
Q: Which dialect uses LEN() instead of LENGTH()?
Ans:SQL Server.
Q: What does TRIM() do by default?
Ans:Removes leading and trailing spaces from a string (some dialects allow trimming other characters).
Q: Does CONCAT() always treat NULL the same way across dialects?
Ans:No — this varies by dialect (e.g. MySQL's CONCAT propagates NULL, PostgreSQL's CONCAT treats NULL as empty string), so behavior must be verified per engine.