SQL Keywords

SQL Keywords

UPPER / LOWER / TRIM / LENGTH / REPLACE

Feb 23, 2026

·

5

min read

Category: String Functions

Platform Support:

✅ Snowflake | ✅ BigQuery | ✅ Databricks

Description

Common string manipulation functions. UPPER converts to uppercase, LOWER to lowercase, TRIM removes whitespace, LENGTH returns string length, and REPLACE substitutes text. Essential for text cleaning, standardization, and data quality.

Syntax

UPPER(string_expression)
LOWER(string_expression)
TRIM([LEADING | TRAILING | BOTH] [characters FROM] string_expression)
LENGTH(string_expression)
REPLACE(string_expression, search_string, replacement_string)
UPPER(string_expression)
LOWER(string_expression)
TRIM([LEADING | TRAILING | BOTH] [characters FROM] string_expression)
LENGTH(string_expression)
REPLACE(string_expression, search_string, replacement_string)
UPPER(string_expression)
LOWER(string_expression)
TRIM([LEADING | TRAILING | BOTH] [characters FROM] string_expression)
LENGTH(string_expression)
REPLACE(string_expression, search_string, replacement_string)

Platform-Specific Notes

All Platforms:

  • Standard SQL string functions

  • Nearly identical syntax and behavior

  • Work with VARCHAR/STRING/TEXT types

  • Handle NULL inputs (return NULL)

Snowflake:

  • CHAR_LENGTH as synonym for LENGTH

  • LTRIM, RTRIM for directional trimming

  • Case-sensitive by default

BigQuery:

  • LENGTH returns bytes for BYTES type

  • CHAR_LENGTH for character count

  • TRIM has flexible syntax

Databricks:

  • Standard Spark SQL functions

  • LTRIM, RTRIM available

  • Compatible with Hive

Example 1: UPPER / LOWER - Case Conversion

All Platforms:

SELECT 
    customer_name,
    UPPER(customer_name) as name_uppercase,
    LOWER(customer_name) as name_lowercase,
    UPPER(email) as email_uppercase,
    LOWER(email) as email_lowercase,
    -- Proper case (first letter uppercase)
    CONCAT(
        UPPER(SUBSTRING(customer_name, 1, 1)),
        LOWER(SUBSTRING(customer_name, 2))
    ) as name_proper_case
FROM

SELECT 
    customer_name,
    UPPER(customer_name) as name_uppercase,
    LOWER(customer_name) as name_lowercase,
    UPPER(email) as email_uppercase,
    LOWER(email) as email_lowercase,
    -- Proper case (first letter uppercase)
    CONCAT(
        UPPER(SUBSTRING(customer_name, 1, 1)),
        LOWER(SUBSTRING(customer_name, 2))
    ) as name_proper_case
FROM

SELECT 
    customer_name,
    UPPER(customer_name) as name_uppercase,
    LOWER(customer_name) as name_lowercase,
    UPPER(email) as email_uppercase,
    LOWER(email) as email_lowercase,
    -- Proper case (first letter uppercase)
    CONCAT(
        UPPER(SUBSTRING(customer_name, 1, 1)),
        LOWER(SUBSTRING(customer_name, 2))
    ) as name_proper_case
FROM

Sample Data (customers table):

customer_id

customer_name

email

1

john SMITH

John.Smith@Email.COM

2

MARIA garcia

MARIA@company.com

3

Sarah Johnson

sarah@DOMAIN.COM

Result:

customer_name

name_uppercase

name_lowercase

email_uppercase

email_lowercase

name_proper_case

john SMITH

JOHN SMITH

john smith

JOHN.SMITH@EMAIL.COM

john.smith@email.com

John smith

MARIA garcia

MARIA GARCIA

maria garcia

MARIA@COMPANY.COM

maria@company.com

Maria garcia

Sarah Johnson

SARAH JOHNSON

sarah johnson

SARAH@DOMAIN.COM

sarah@domain.com

Sarah johnson

Example 2: TRIM - Remove Whitespace

All Platforms:

SELECT 
    product_code,
    LENGTH(product_code) as original_length,
    TRIM(product_code) as trimmed,
    LENGTH(TRIM(product_code)) as trimmed_length,
    -- Remove specific characters
    TRIM(BOTH '-' FROM product_code) as trim_dashes,
    -- Left trim only
    LTRIM(product_code) as left_trimmed,
    -- Right trim only
    RTRIM(product_code) as right_trimmed
FROM

SELECT 
    product_code,
    LENGTH(product_code) as original_length,
    TRIM(product_code) as trimmed,
    LENGTH(TRIM(product_code)) as trimmed_length,
    -- Remove specific characters
    TRIM(BOTH '-' FROM product_code) as trim_dashes,
    -- Left trim only
    LTRIM(product_code) as left_trimmed,
    -- Right trim only
    RTRIM(product_code) as right_trimmed
FROM

SELECT 
    product_code,
    LENGTH(product_code) as original_length,
    TRIM(product_code) as trimmed,
    LENGTH(TRIM(product_code)) as trimmed_length,
    -- Remove specific characters
    TRIM(BOTH '-' FROM product_code) as trim_dashes,
    -- Left trim only
    LTRIM(product_code) as left_trimmed,
    -- Right trim only
    RTRIM(product_code) as right_trimmed
FROM

Sample Data (products table):

product_id

product_code

1

" ABC123 "

2

"XYZ789 "

3

" -PROD- "

Result:

product_code

original_length

trimmed

trimmed_length

trim_dashes

left_trimmed

right_trimmed

" ABC123 "

10

"ABC123"

6

"ABC123"

"ABC123 "

" ABC123"

"XYZ789 "

9

"XYZ789"

6

"XYZ789"

"XYZ789 "

"XYZ789"

" -PROD- "

10

"-PROD-"

6

" PROD "

"-PROD- "

" -PROD-"

Example 3: LENGTH - String Length

All Platforms:

SELECT 
    description,
    LENGTH(description) as char_count,
    LENGTH(TRIM(description)) as char_count_trimmed,
    -- Check for empty or whitespace-only strings
    CASE 
        WHEN LENGTH(TRIM(description)) = 0 THEN 'Empty'
        WHEN LENGTH(TRIM(description)) < 10 THEN 'Short'
        WHEN LENGTH(TRIM(description)) < 50 THEN 'Medium'
        ELSE 'Long'
    END as description_length_category,
    -- Truncate long descriptions
    CASE 
        WHEN LENGTH(description) > 50 
        THEN SUBSTRING(description, 1, 47) || '...'
        ELSE description
    END as truncated_description
FROM

SELECT 
    description,
    LENGTH(description) as char_count,
    LENGTH(TRIM(description)) as char_count_trimmed,
    -- Check for empty or whitespace-only strings
    CASE 
        WHEN LENGTH(TRIM(description)) = 0 THEN 'Empty'
        WHEN LENGTH(TRIM(description)) < 10 THEN 'Short'
        WHEN LENGTH(TRIM(description)) < 50 THEN 'Medium'
        ELSE 'Long'
    END as description_length_category,
    -- Truncate long descriptions
    CASE 
        WHEN LENGTH(description) > 50 
        THEN SUBSTRING(description, 1, 47) || '...'
        ELSE description
    END as truncated_description
FROM

SELECT 
    description,
    LENGTH(description) as char_count,
    LENGTH(TRIM(description)) as char_count_trimmed,
    -- Check for empty or whitespace-only strings
    CASE 
        WHEN LENGTH(TRIM(description)) = 0 THEN 'Empty'
        WHEN LENGTH(TRIM(description)) < 10 THEN 'Short'
        WHEN LENGTH(TRIM(description)) < 50 THEN 'Medium'
        ELSE 'Long'
    END as description_length_category,
    -- Truncate long descriptions
    CASE 
        WHEN LENGTH(description) > 50 
        THEN SUBSTRING(description, 1, 47) || '...'
        ELSE description
    END as truncated_description
FROM

Sample Data (products table):

product_id

description

1

"Laptop"

2

"High-performance wireless mouse with ergonomic design"

3

" "

4

NULL

Result:

description

char_count

char_count_trimmed

description_length_category

truncated_description

Laptop

6

6

Short

Laptop

High-performance...

54

54

Long

High-performance wireless mouse with ergo...

" "

3

0

Empty

" "

NULL

NULL

NULL

NULL

NULL

Example 4: REPLACE - Text Substitution

All Platforms:

SELECT 
    phone_number,
    -- Remove formatting characters
    REPLACE(REPLACE(REPLACE(phone_number, '(', ''), ')', ''), '-', '') as clean_phone,
    -- Standardize format
    REPLACE(REPLACE(phone_number, '.', '-'), '/', '-') as standardized_phone,
    -- Mask sensitive data
    CONCAT(
        SUBSTRING(phone_number, 1, 3),
        REPLACE(SUBSTRING(phone_number, 4), SUBSTRING(phone_number, 4, LENGTH(phone_number) - 6), 'XXX')
    ) as masked_phone
FROM

SELECT 
    phone_number,
    -- Remove formatting characters
    REPLACE(REPLACE(REPLACE(phone_number, '(', ''), ')', ''), '-', '') as clean_phone,
    -- Standardize format
    REPLACE(REPLACE(phone_number, '.', '-'), '/', '-') as standardized_phone,
    -- Mask sensitive data
    CONCAT(
        SUBSTRING(phone_number, 1, 3),
        REPLACE(SUBSTRING(phone_number, 4), SUBSTRING(phone_number, 4, LENGTH(phone_number) - 6), 'XXX')
    ) as masked_phone
FROM

SELECT 
    phone_number,
    -- Remove formatting characters
    REPLACE(REPLACE(REPLACE(phone_number, '(', ''), ')', ''), '-', '') as clean_phone,
    -- Standardize format
    REPLACE(REPLACE(phone_number, '.', '-'), '/', '-') as standardized_phone,
    -- Mask sensitive data
    CONCAT(
        SUBSTRING(phone_number, 1, 3),
        REPLACE(SUBSTRING(phone_number, 4), SUBSTRING(phone_number, 4, LENGTH(phone_number) - 6), 'XXX')
    ) as masked_phone
FROM

Sample Data (contacts table):

contact_id

phone_number

1

(555) 123-4567

2

555.987.6543

3

555/456/7890

Result:

phone_number

clean_phone

standardized_phone

masked_phone

(555) 123-4567

5551234567

555-123-4567

555XXX4567

555.987.6543

5559876543

555-987-6543

555XXX6543

555/456/7890

5554567890

555-456-7890

555XXX7890

Example 5: Data Cleaning Pipeline

All Platforms:

SELECT 
    customer_name_raw,
    -- Clean and standardize name
    TRIM(
        UPPER(
            REPLACE(
                REPLACE(
                    REPLACE(customer_name_raw, '  ', ' '),  -- Double spaces to single
                    ',', ''),  -- Remove commas
                '.', '')  -- Remove periods
        )
    ) as customer_name_clean,
    
    email_raw,
    -- Clean and standardize email
    LOWER(TRIM(email_raw)) as email_clean,
    
    -- Validate email format (basic check)
    CASE 
        WHEN LOWER(TRIM(email_raw)) LIKE '%@%.%' 
        AND LENGTH(TRIM(email_raw)) > 5
        THEN 'Valid'
        ELSE 'Invalid'
    END as email_status
FROM

SELECT 
    customer_name_raw,
    -- Clean and standardize name
    TRIM(
        UPPER(
            REPLACE(
                REPLACE(
                    REPLACE(customer_name_raw, '  ', ' '),  -- Double spaces to single
                    ',', ''),  -- Remove commas
                '.', '')  -- Remove periods
        )
    ) as customer_name_clean,
    
    email_raw,
    -- Clean and standardize email
    LOWER(TRIM(email_raw)) as email_clean,
    
    -- Validate email format (basic check)
    CASE 
        WHEN LOWER(TRIM(email_raw)) LIKE '%@%.%' 
        AND LENGTH(TRIM(email_raw)) > 5
        THEN 'Valid'
        ELSE 'Invalid'
    END as email_status
FROM

SELECT 
    customer_name_raw,
    -- Clean and standardize name
    TRIM(
        UPPER(
            REPLACE(
                REPLACE(
                    REPLACE(customer_name_raw, '  ', ' '),  -- Double spaces to single
                    ',', ''),  -- Remove commas
                '.', '')  -- Remove periods
        )
    ) as customer_name_clean,
    
    email_raw,
    -- Clean and standardize email
    LOWER(TRIM(email_raw)) as email_clean,
    
    -- Validate email format (basic check)
    CASE 
        WHEN LOWER(TRIM(email_raw)) LIKE '%@%.%' 
        AND LENGTH(TRIM(email_raw)) > 5
        THEN 'Valid'
        ELSE 'Invalid'
    END as email_status
FROM

Sample Data (customer_import table):

customer_id

customer_name_raw

email_raw

1

" John Smith "

" John@Email.COM "

2

"Garcia, Maria"

"MARIA@company"

3

"Dr. Sarah Johnson"

"sarah@domain.com"

Result:

customer_name_raw

customer_name_clean

email_raw

email_clean

email_status

" John Smith "

"JOHN SMITH"

" John@Email.COM "

"john@email.com"

Valid

"Garcia, Maria"

"GARCIA MARIA"

"MARIA@company"

"maria@company"

Invalid

"Dr. Sarah Johnson"

"DR SARAH JOHNSON"

"sarah@domain.com"

"sarah@domain.com"

Valid

Example 6: Combining All Functions

All Platforms:

SELECT 
    product_name,
    -- Create SKU from product name
    CONCAT(
        UPPER(SUBSTRING(REPLACE(TRIM(product_name), ' ', ''), 1, 3)),
        '-',
        LPAD(CAST(product_id AS VARCHAR), 5, '0')
    ) as generated_sku,
    
    -- Clean description
    CASE 
        WHEN LENGTH(TRIM(description)) > 0 THEN
            CONCAT(
                UPPER(SUBSTRING(TRIM(description), 1, 1)),
                LOWER(SUBSTRING(TRIM(description), 2))
            )
        ELSE 'No description'
    END as clean_description,
    
    -- Category standardization
    UPPER(REPLACE(TRIM(category), '_', ' ')) as category_standardized
FROM

SELECT 
    product_name,
    -- Create SKU from product name
    CONCAT(
        UPPER(SUBSTRING(REPLACE(TRIM(product_name), ' ', ''), 1, 3)),
        '-',
        LPAD(CAST(product_id AS VARCHAR), 5, '0')
    ) as generated_sku,
    
    -- Clean description
    CASE 
        WHEN LENGTH(TRIM(description)) > 0 THEN
            CONCAT(
                UPPER(SUBSTRING(TRIM(description), 1, 1)),
                LOWER(SUBSTRING(TRIM(description), 2))
            )
        ELSE 'No description'
    END as clean_description,
    
    -- Category standardization
    UPPER(REPLACE(TRIM(category), '_', ' ')) as category_standardized
FROM

SELECT 
    product_name,
    -- Create SKU from product name
    CONCAT(
        UPPER(SUBSTRING(REPLACE(TRIM(product_name), ' ', ''), 1, 3)),
        '-',
        LPAD(CAST(product_id AS VARCHAR), 5, '0')
    ) as generated_sku,
    
    -- Clean description
    CASE 
        WHEN LENGTH(TRIM(description)) > 0 THEN
            CONCAT(
                UPPER(SUBSTRING(TRIM(description), 1, 1)),
                LOWER(SUBSTRING(TRIM(description), 2))
            )
        ELSE 'No description'
    END as clean_description,
    
    -- Category standardization
    UPPER(REPLACE(TRIM(category), '_', ' ')) as category_standardized
FROM

Sample Data:

product_id

product_name

description

category

1

" Wireless Mouse "

" ERGONOMIC DESIGN"

"computer_accessories"

2

"USB Cable"

""

"cables_adapters"

Result:

product_name

generated_sku

clean_description

category_standardized

" Wireless Mouse "

"WIR-00001"

"Ergonomic design"

"COMPUTER ACCESSORIES"

"USB Cable"

"USB-00002"

"No description"

"CABLES ADAPTERS"

Common String Function Patterns

Email standardization:

LOWER(TRIM(email)) as
LOWER(TRIM(email)) as
LOWER(TRIM(email)) as

Remove multiple spaces:

REGEXP_REPLACE(text, '\s+', ' ')  -- Replace multiple whitespace with single space
REGEXP_REPLACE(text, '\s+', ' ')  -- Replace multiple whitespace with single space
REGEXP_REPLACE(text, '\s+', ' ')  -- Replace multiple whitespace with single space

Phone number cleaning:

REPLACE(REPLACE(REPLACE(REPLACE(phone, '(', ''), ')', ''), '-', ''), ' ', '')
REPLACE(REPLACE(REPLACE(REPLACE(phone, '(', ''), ')', ''), '-', ''), ' ', '')
REPLACE(REPLACE(REPLACE(REPLACE(phone, '(', ''), ')', ''), '-', ''), ' ', '')

Title case (first letter of each word):

-- Platform-specific, or use INITCAP in some platforms
CONCAT(
    UPPER(SUBSTRING(word, 1, 1)),
    LOWER(SUBSTRING(word, 2))
)
-- Platform-specific, or use INITCAP in some platforms
CONCAT(
    UPPER(SUBSTRING(word, 1, 1)),
    LOWER(SUBSTRING(word, 2))
)
-- Platform-specific, or use INITCAP in some platforms
CONCAT(
    UPPER(SUBSTRING(word, 1, 1)),
    LOWER(SUBSTRING(word, 2))
)

Use Cases by Function

UPPER/LOWER:

  1. Case-insensitive comparison: WHERE UPPER(email) = UPPER(@search)

  2. Standardization: Consistent data format

  3. Display formatting: UI requirements

  4. Data matching: Fuzzy matching

TRIM:

  1. Data cleaning: Remove accidental whitespace

  2. Import validation: Clean uploaded data

  3. Comparison: Ensure accurate matching

  4. Storage optimization: Remove unnecessary characters

LENGTH:

  1. Validation: Check min/max lengths

  2. Truncation: Decide if text needs shortening

  3. Data quality: Find empty or suspiciously short/long values

  4. Categorization: Group by text length

REPLACE:

  1. Formatting removal: Clean phone numbers, codes

  2. Character substitution: Standardize separators

  3. Censoring: Mask sensitive data

  4. Data correction: Fix common typos

Performance Considerations

  1. Avoid in WHERE clauses: Prevents index usage

  2. Pre-compute when possible: Create computed columns

  3. Bulk operations: Apply transformations during ETL

  4. Regular expressions: Use for complex patterns (but slower)

Best Practices

  1. Normalize during insert: Clean data at entry point

  2. Store cleaned versions: Separate columns for display vs search

  3. Handle NULLs: Use COALESCE for NULL strings

  4. Test edge cases: Empty strings, special characters, Unicode

  5. Document transformations: Explain cleaning logic

  6. Validate after cleaning: Ensure data quality

  7. Use consistent casing: Pick UPPER or LOWER for standards

  8. Chain functions carefully: Test each step independently

Interested to Learn More?
Try Out the Free 14-Days Trial

More Articles

decorative icon

Experience Analytics for the AI-Era

Start your 14-day trial today - it's free and no credit card needed

decorative icon

Experience Analytics for the AI-Era

Start your 14-day trial today - it's free and no credit card needed

decorative icon

Experience Analytics for the AI-Era

Start your 14-day trial today - it's free and no credit card needed

Copyright © 2026 Paradime Labs, Inc.

Made with ❤️ in San Francisco ・ London

*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.

Copyright © 2026 Paradime Labs, Inc.

Made with ❤️ in San Francisco ・ London

*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.

Copyright © 2026 Paradime Labs, Inc.

Made with ❤️ in San Francisco ・ London

*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.