First Letter Capitalization II - Problem

You work for a content management system that needs to properly format user-generated text. Your task is to implement a smart text capitalization system that transforms text according to professional publishing standards.

Given a database table user_content with user-generated text, you need to:

  • Capitalize the first letter of each word and make remaining letters lowercase
  • Handle hyphenated words specially: Both parts of hyphenated words should be capitalized (e.g., top-rated โ†’ Top-Rated)
  • Preserve all spacing and formatting

The table structure:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| content_id  | int     |
| content_text| varchar |
+-------------+---------+

Return both the original and transformed text for comparison.

Input & Output

example_1.sql โ€” Basic Capitalization
$ Input: user_content: | content_id | content_text | |------------|-------------| | 1 | "hello world" |
โ€บ Output: | content_id | content_text | capitalized_text | |------------|--------------|------------------| | 1 | "hello world" | "Hello World" |
๐Ÿ’ก Note: Each word's first letter is capitalized: 'hello world' becomes 'Hello World'. The space is preserved between words.
example_2.sql โ€” Hyphenated Words
$ Input: user_content: | content_id | content_text | |------------|-------------| | 2 | "top-rated product" |
โ€บ Output: | content_id | content_text | capitalized_text | |------------|--------------|------------------| | 2 | "top-rated product" | "Top-Rated Product" |
๐Ÿ’ก Note: Hyphenated words get both parts capitalized: 'top-rated' becomes 'Top-Rated', and 'product' becomes 'Product'.
example_3.sql โ€” Mixed Case Edge Cases
$ Input: user_content: | content_id | content_text | |------------|-------------| | 3 | "HTML-based WEB development" |
โ€บ Output: | content_id | content_text | capitalized_text | |------------|--------------|------------------| | 3 | "HTML-based WEB development" | "Html-Based Web Development" |
๐Ÿ’ก Note: All words are normalized: 'HTML-based' becomes 'Html-Based', 'WEB' becomes 'Web', and 'development' becomes 'Development'.

Constraints

  • 1 โ‰ค number of rows โ‰ค 104
  • 1 โ‰ค length of content_text โ‰ค 1000
  • content_text contains only English letters, spaces, and hyphens
  • No leading or trailing spaces in individual words
  • Hyphens only appear between letters (not at start/end of words)

Visualization

Tap to expand
Text Capitalization PipelineINPUT TEXT"hello-world"Raw contentANALYZEFind boundariesDetect patternsTRANSFORMApply rulesCapitalizeOUTPUT"Hello-World"Formatted textDetailed Processing Steps1Scan for word boundariesIdentify spaces and hyphens as delimiters2Mark capitalization positionsFirst char and chars after delimiters3Apply transformation rulesUPPER() for marked positions, LOWER() for others4Preserve original formattingKeep spaces, hyphens, and structure intactExample Trace: "top-rated service"tโ†’T (start), oโ†’o, pโ†’p, -โ†’-, rโ†’R (after hyphen), aโ†’a, tโ†’t, eโ†’e, dโ†’d, space, sโ†’S (after space), eโ†’e, ...Result: "Top-Rated Service"
Understanding the Visualization
1
Input Analysis
Scan the text to identify word boundaries and special characters
2
Pattern Detection
Find positions where capitalization should occur (start of words, after hyphens)
3
Transform Application
Apply capitalization rules while preserving original spacing and formatting
4
Result Assembly
Combine transformed characters into the final formatted text
Key Takeaway
๐ŸŽฏ Key Insight: Using regex patterns `(^|[ ])([a-z])` and `(-)([a-z])` allows SQL to identify and transform characters in a single pass, making this both efficient and database-optimized.
Asked in
Meta 25 Amazon 18 Google 15 Microsoft 12
28.4K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen