SQL: Structured Query LanguageSQL FundamentalsScalar Functions
How can SQL functions be used to change text case?
Question 29 of 30 · Updated
On this page
SQL provides UPPER() and LOWER() to convert text to uppercase and lowercase. PostgreSQL also provides INITCAP() to capitalize the first letter of each word and lowercase the remaining letters. These functions are useful for consistent display labels, product codes and text comparisons.
Practical example: format product names#
Imagine a product catalog with inconsistent capitalization. Instead of changing stored names, you can format them when displaying query results. Run this PostgreSQL example with sample data:
SQLWITH products(id, name) AS ( VALUES (1, 'blue PEN'), (2, 'NOTEBOOK'), (3, NULL::text) ) SELECT id, name, UPPER(name) AS uppercase_name, LOWER(name) AS lowercase_name, INITCAP(name) AS title_style FROM products ORDER BY id;
| id | name | uppercase_name | lowercase_name | title_style |
|---|---|---|---|---|
| 1 | blue PEN | BLUE PEN | blue pen | Blue Pen |
| 2 | NOTEBOOK | NOTEBOOK | notebook | Notebook |
| 3 | NULL | NULL | NULL | NULL |
Each function returns a transformed value for the current row. The original name remains unchanged. UPPER and LOWER are widely supported; INITCAP availability varies by database.
Another use: compare or format codes#
SQLSELECT LOWER('SAVE10') = LOWER('save10') AS same_code, UPPER('sku-42') AS sku, LOWER('') AS empty_text, INITCAP('USB cable') AS title_style;
| same_code | sku | empty_text | title_style |
|---|---|---|---|
| true | SKU-42 | (empty string) | Usb Cable |
Converting both coupon codes to lowercase makes this comparison match despite different capitalization. Use this approach only when the business rule treats case differences as irrelevant. PostgreSQL also supports ILIKE for case-insensitive pattern matching; unlike UPPER or LOWER, it tests a pattern rather than returning transformed text.
Common mistakes to avoid#
- INITCAP is not a smart title formatter: it changes USB to Usb. Preserve brand names and acronyms when their original spelling matters.
- Case conversion does not trim spaces or remove accents. Results for non-ASCII characters may depend on the database locale and collation.
- NULL remains NULL, whereas an empty string remains empty. A SELECT expression does not permanently update the table.
Interview takeaway: UPPER converts text to uppercase, LOWER to lowercase, and PostgreSQL INITCAP capitalizes words. Choose the function based on the required output or comparison, not merely to make every stored value look uniform.
Validation: Both SQL examples were executed successfully on PostgreSQL with synthetic data. Output tables are sufficient; no diagram is needed. Official Supabase documentation was consulted for ILIKE; the PostgreSQL reference was not live fetched under the offline research exception.
References: PostgreSQL string functions and operators · Supabase Query logs with SQL
References
- 1.PostgreSQL — String Functions and Operatorspostgresql.org
- 2.Supabase — Query logs with SQLsupabase.com
Related questions
Explore other topics:Forward Deployed EngineeringPythonAI System DesignMachine Learning