First Letter Capitalization II - Problem

You have a table user_content containing text content that needs to be properly formatted.

Table: user_content

Column NameType
content_idint
content_textvarchar

content_id is the unique key for this table. Each row contains a unique ID and the corresponding text content.

Write a solution to transform the text in the content_text column by applying the following rules:

  • Convert the first letter of each word to uppercase and the remaining letters to lowercase
  • Special handling for words containing special characters:
    • For words connected with a hyphen -, both parts should be capitalized (e.g., top-ratedTop-Rated)
  • All other formatting and spacing should remain unchanged

Return the result table that includes both the original content_text and the modified text following the above rules.

Table Schema

user_content
Column Name Type Description
content_id PK int Unique identifier for each content row
content_text varchar Text content to be formatted
Primary Key: content_id
Note: Each row contains unique text content that may include hyphenated words

Input & Output

Example 1 — Basic Text with Hyphenated Words
Input Table:
content_id content_text
1 hello world-class service
2 top-rated product review
3 user-friendly interface design
Output:
content_id original_text formatted_text
1 hello world-class service Hello World-Class Service
2 top-rated product review Top-Rated Product Review
3 user-friendly interface design User-Friendly Interface Design
💡 Note:

Each word's first letter is capitalized, and for hyphenated words like 'world-class', both parts are capitalized as 'World-Class'.

Example 2 — Mixed Case and Special Formatting
Input Table:
content_id content_text
4 API documentation
5 self-service portal
6 twenty-four hour support
Output:
content_id original_text formatted_text
4 API documentation Api Documentation
5 self-service portal Self-Service Portal
6 twenty-four hour support Twenty-Four Hour Support
💡 Note:

All text is converted to proper case with first letters capitalized. Note that 'API' becomes 'Api' as the rule converts to lowercase first, then capitalizes first letters.

Constraints

  • 1 ≤ content_id ≤ 1000
  • content_text contains only letters, spaces, and hyphens
  • 1 ≤ length(content_text) ≤ 500

Visualization

Tap to expand
Text Capitalization Problem OverviewInput Tablecontent_idcontent_text1hello world-class2top-rated serviceREGEXP_REPLACEPattern: \b([a-z]) → \U\1Pattern: (-[a-z]) → \U\1Output Resultcontent_idformatted_text1Hello World-Class2Top-Rated Service
Understanding the Visualization
1
Input
Original text with mixed case
2
Process
Apply REGEXP_REPLACE patterns
3
Output
Properly capitalized text
Key Takeaway
🎯 Key Insight: Use regex patterns to handle both regular word boundaries and special cases like hyphenated words
Asked in
Meta 28 Amazon 22 Google 18
25.4K Views
Medium Frequency
~18 min Avg. Time
890 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