<aside> 🔤 String functions manipulate, transform, and extract text data.

</aside>


1. CONCAT() — Combine Strings

SELECT
    CONCAT(first_name, '-', country) AS full_info
FROM customers

2. LOWER() & UPPER() — Change Case

-- Convert to lowercase
SELECT LOWER(first_name) AS lower_name FROM customers

-- Convert to uppercase
SELECT UPPER(first_name) AS upper_name FROM customers

3. TRIM() — Remove Whitespace

Removes leading and trailing spaces from a string.

-- Find customers with extra spaces in their name
SELECT
    first_name,
    LEN(first_name)       AS len_name,
    LEN(TRIM(first_name)) AS len_trim_name,
    LEN(first_name) - LEN(TRIM(first_name)) AS flag
FROM customers
WHERE LEN(first_name) != LEN(TRIM(first_name))

4. REPLACE() — Find and Replace

-- Replace dashes with slashes in phone number
SELECT
    '123-456-7890' AS phone,
    REPLACE('123-456-7890', '-', '/') AS clean_phone

-- Change file extension from .txt to .csv
SELECT
    'report.txt' AS old_filename,
    REPLACE('report.txt', '.txt', '.csv') AS new_filename

5. LEN() — String Length

SELECT
    first_name,
    LEN(first_name) AS name_length
FROM customers

6. LEFT() & RIGHT() — Extract from Start or End

-- First 2 characters
SELECT first_name, LEFT(TRIM(first_name), 2) AS first_2
FROM customers

-- Last 2 characters
SELECT first_name, RIGHT(first_name, 2) AS last_2
FROM customers