Loading lesson...
Slice, search, and reshape text at will
SUBSTR, TRIM, and string surgery 101
Topics covered: SUBSTRING(), Edge Extractions, REPLACE(), STRPOS(), TRIM()
Basic Extraction Consider product codes that embed category information in specific positions. Extract to String End Practical Applications Parsing Email Domains This query finds the position of the @ symbol, adds 1 to skip past it, then extracts everything from that point to the end. The domain portion has variable length, so omitting the length parameter is essential. Fixed-Width Extraction Many legacy systems use fixed-width formats where each field occupies specific character positions: Best
Extract from Start RIGHT() Extracting from the end requires calculating the start position or using negative indices, depending on your database. Extracting from the End Comparison & Best Practices Understanding the differences between extraction methods helps you choose the right approach for each situation. Extraction Patterns Practical Examples Extracting order year and sequence number from order IDs: Extracting Prefixes and Suffixes Extracting from the end of a string with a negative start p
Basic Replacement Standardizing company names by expanding abbreviations. Removing Characters To remove characters, replace them with an empty string: Advanced Usage Data Standardization Date formats with slashes are converted to ISO format with hyphens. This standardization is critical when importing data from systems with different conventions. Single vs Chained Multiple Replacements Best Practices When to Use REPLACE()
Delimiter Positions Extraction by Position Advanced Usage Substring Existence STRPOS returns 0 when not found, but returns 1 for a match at the beginning. Remember that SQL positions start at 1, so a match at the first character returns 1, not 0. Multiple Delimiters Best Practices When to Use STRPOS()
Removing Whitespace Whitespace problems are invisible but cause real bugs. Extra spaces prevent joins from matching and corrupt comparisons. LTRIM() and RTRIM() Custom Character Trim You can specify which characters to remove instead of whitespace: Practical Applications Cleaning Import Data Data imported from spreadsheets or external systems often has invisible whitespace that breaks joins: Import vs Query TRIM Best Practices When to Use TRIM() These are the most common scenarios where whitespa