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.
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 stringsCASEWHEN LENGTH(TRIM(description)) = 0THEN'Empty'WHEN LENGTH(TRIM(description)) < 10THEN'Short'WHEN LENGTH(TRIM(description)) < 50THEN'Medium'ELSE'Long'ENDas description_length_category,-- Truncate long descriptionsCASEWHEN LENGTH(description) > 50THEN SUBSTRING(description,1,47) || '...'ELSE description
ENDas truncated_description
FROM
SELECT
description,
LENGTH(description)as char_count,
LENGTH(TRIM(description))as char_count_trimmed,-- Check for empty or whitespace-only stringsCASEWHEN LENGTH(TRIM(description)) = 0THEN'Empty'WHEN LENGTH(TRIM(description)) < 10THEN'Short'WHEN LENGTH(TRIM(description)) < 50THEN'Medium'ELSE'Long'ENDas description_length_category,-- Truncate long descriptionsCASEWHEN LENGTH(description) > 50THEN SUBSTRING(description,1,47) || '...'ELSE description
ENDas truncated_description
FROM
SELECT
description,
LENGTH(description)as char_count,
LENGTH(TRIM(description))as char_count_trimmed,-- Check for empty or whitespace-only stringsCASEWHEN LENGTH(TRIM(description)) = 0THEN'Empty'WHEN LENGTH(TRIM(description)) < 10THEN'Short'WHEN LENGTH(TRIM(description)) < 50THEN'Medium'ELSE'Long'ENDas description_length_category,-- Truncate long descriptionsCASEWHEN LENGTH(description) > 50THEN SUBSTRING(description,1,47) || '...'ELSE description
ENDas 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)CASEWHEN LOWER(TRIM(email_raw))LIKE'%@%.%'AND LENGTH(TRIM(email_raw)) > 5THEN'Valid'ELSE'Invalid'ENDas 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)CASEWHEN LOWER(TRIM(email_raw))LIKE'%@%.%'AND LENGTH(TRIM(email_raw)) > 5THEN'Valid'ELSE'Invalid'ENDas 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)CASEWHEN LOWER(TRIM(email_raw))LIKE'%@%.%'AND LENGTH(TRIM(email_raw)) > 5THEN'Valid'ELSE'Invalid'ENDas email_status
FROM
*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.
*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.
*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.