TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2746 987
๐Ÿš€ SQL Roadmap 2026 โ€” Part 9

SQL String Functions โ€” Cleaning & Transforming Text Data

In real-world databases, a huge amount of information is stored as text:

โ€ข Customer names

โ€ข Email addresses

โ€ข Phone numbers

โ€ข Product names

โ€ข Cities

โ€ข Categories

โ€ข Addresses

โ€ข Job titles

But text data is rarely perfectly clean.

You may encounter:

โ€ข ' Alice '

โ€ข 'alice@example.com'

โ€ข 'ALICE@EXAMPLE.COM'

โ€ข 'Premium Customer'

โ€ข ' Mumbai'

SQL string functions allow you to clean, search, extract, combine, and transform text directly inside your queries.

๐Ÿง  1. What Are String Functions?

String functions are SQL functions that operate on text values.

Common functions include:

โ€ข LENGTH()

โ€ข UPPER()

โ€ข LOWER()

โ€ข TRIM()

โ€ข LTRIM()

โ€ข RTRIM()

โ€ข SUBSTRING()

โ€ข LEFT()

โ€ข RIGHT()

โ€ข CONCAT()

โ€ข REPLACE()

โ€ข POSITION()

โ€ข CHAR_LENGTH()

Exact function names and syntax can vary slightly between databases such as PostgreSQL, MySQL, SQL Server, and Oracle.

๐Ÿ”  2. UPPER()

Converts text to uppercase.

SELECT
customer_name,
UPPER(customer_name) AS uppercase_name
FROM customers;


Example:

Alice becomes ALICE

Useful for:

โ€ข Standardizing text

โ€ข Case-insensitive comparisons

โ€ข Creating reports

โ€ข Data cleaning

๐Ÿ”ก 3. LOWER()

Converts text to lowercase.

SELECT
LOWER(email) AS email
FROM customers;


Example:

ALICE@EXAMPLE.COM becomes alice@example.com

A common data-cleaning pattern is:

SELECT
LOWER(TRIM(email)) AS cleaned_email
FROM customers;


This handles both unnecessary spaces and inconsistent capitalization.

๐Ÿงน 4. TRIM()

Removes leading and trailing spaces.

SELECT
TRIM(customer_name) AS cleaned_name
FROM customers;


For example ' Alice ' becomes 'Alice'

This is extremely useful when importing data from Excel, CSV files, APIs, and external systems.

โ†ฉ๏ธ 5. LTRIM() and RTRIM()

โ€ข LTRIM() removes spaces from the beginning:

  SELECT LTRIM(customer_name) FROM customers;


โ€ข RTRIM() removes spaces from the end:

  SELECT RTRIM(customer_name) FROM customers;


โ€ข While TRIM() generally handles both sides:

  SELECT TRIM(customer_name) FROM customers;


๐Ÿ“ 6. LENGTH()

Returns the number of characters in a string.

SELECT
customer_name,
LENGTH(customer_name) AS name_length
FROM customers;


Example:

โ€ข Alice โ†’ 5

โ€ข Robert โ†’ 6

Function behavior can vary across SQL dialects, particularly with multibyte characters.

๐Ÿ” 7. Finding Long or Short Values

String length can be useful for data-quality checks.

Example:

SELECT * FROM customers WHERE LENGTH(phone) < 10;


This can help identify potentially invalid phone numbers.

SELECT * FROM products WHERE LENGTH(product_name) > 100;


This can identify unusually long product descriptions.

โœ‚๏ธ 8. SUBSTRING()

SUBSTRING() extracts part of a string.

A common form is: SUBSTRING(column_name, start_position, length)

SELECT SUBSTRING(customer_name, 1, 3) AS first_three_characters FROM customers;


For Alexander the result would be Ale.

Syntax differs by database, so always check the dialect you're using.

๐Ÿ‘ˆ 9. LEFT()

Returns characters from the beginning of a string.
  • โค 1
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’