#3374
Hard Database First letter capitalization ii
Database
56.6% acceptance
Feb 27, 2026
31
16
Table: user_content
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| content_id | int |
| content_text| varchar |
+-------------+---------+
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-rated → Top-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.
The result format is in the following example.
Solution
SQL
#
# Table: user_content
# +-------------+---------+
# | Column Name | Type |
# +-------------+---------+
# | content_id | int |
# | content_text| varchar |
# +-------------+---------+
# 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-rated → Top-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.
# The result format is in the following example.
# Example:
# Input:
# user_content table:
# +------------+---------------------------------+
# | content_id | content_text |
# +------------+---------------------------------+
# | 1 | hello world of SQL |
# | 2 | the QUICK-brown fox |
# | 3 | modern-day DATA science |
# | 4 | web-based FRONT-end development |
# +------------+---------------------------------+
# Output:
# +------------+---------------------------------+---------------------------------+
# | content_id | original_text | converted_text |
# +------------+---------------------------------+---------------------------------+
# | 1 | hello world of SQL | Hello World Of Sql |
# | 2 | the QUICK-brown fox | The Quick-Brown Fox |
# | 3 | modern-day DATA science | Modern-Day Data Science |
# | 4 | web-based FRONT-end development | Web-Based Front-End Development |
# +------------+---------------------------------+---------------------------------+
# Explanation:
# For content_id = 1:
# Each word's first letter is capitalized: "Hello World Of Sql"
# For content_id = 2:
# Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown"
# Other words follow normal capitalization rules
# For content_id = 3:
# Hyphenated word "modern-day" becomes "Modern-Day"
# "DATA" is converted to "Data"
# For content_id = 4:
# Contains two hyphenated words: "web-based" → "Web-Based"
# And "FRONT-end" → "Front-End"
# Constraints:
# context_text contains only English letters, and the characters in the list ['\', ' ', '@', '-', '/', '^', ',']
#
# Test case:
# CREATE TABLE user_content (
# content_id INT,
# content_text VARCHAR(255)
# );
# INSERT INTO user_content VALUES
# (1, 'hello world of SQL'),
# (2, 'the QUICK-brown fox'),
# (3, 'modern-day DATA science'),
# (4, 'foo--bar -baz lOO-daR-@Daz-');
#
# Expected output:
# +------------+---------------------------------+---------------------------------+
# | content_id | original_text | converted_text |
# +------------+---------------------------------+---------------------------------+
# | 1 | hello world of SQL | Hello World Of Sql |
# | 2 | the QUICK-brown fox | The Quick-Brown Fox |
# | 3 | modern-day DATA science | Modern-Day Data Science |
# | 4 | foo--bar -baz lOO-daR-@Daz- | Foo--bar -baz Loo-dar-@daz- |
# +------------+---------------------------------+---------------------------------+
# Write your MySQL query statement below
# Note: REGEXP_REPLACE cannot apply UPPER() to captured groups in MySQL.
# Use a numbers CTE (1..N) joined to character positions, then GROUP_CONCAT
# to rebuild the string. Capitalize at position 1 or after a space unconditionally.
# Capitalize after '-' only when: the char before '-' is a letter AND the
# entire space-delimited word matches ^[a-zA-Z]+(-[a-zA-Z]+)+$ (i.e. it is
# composed purely of letter-segments joined by single hyphens with no empty
# parts or special characters). This avoids capitalizing after '--', after
# a leading '-', or inside words that contain non-letter/non-hyphen chars.
WITH RECURSIVE nums AS (
SELECT 1 AS n
UNION ALL
SELECT n + 1 FROM nums WHERE n < 1000
)
SELECT
uc.content_id,
uc.content_text AS original_text,
GROUP_CONCAT(
CASE
WHEN nums.n = 1
OR SUBSTRING(uc.content_text, nums.n - 1, 1) = ' '
THEN UPPER(SUBSTRING(LOWER(uc.content_text), nums.n, 1))
WHEN
SUBSTRING(uc.content_text, nums.n - 1, 1) = '-'
AND SUBSTRING(LOWER(uc.content_text), nums.n - 2, 1) REGEXP '[a-z]'
AND SUBSTRING_INDEX(
SUBSTRING_INDEX(
uc.content_text, ' ',
CHAR_LENGTH(SUBSTRING(uc.content_text, 1, nums.n - 1))
- CHAR_LENGTH(REPLACE(SUBSTRING(uc.content_text, 1, nums.n - 1), ' ', ''))
+ 1
), ' ', -1
) REGEXP '^[a-zA-Z]+(-[a-zA-Z]+)+$'
THEN UPPER(SUBSTRING(LOWER(uc.content_text), nums.n, 1))
ELSE SUBSTRING(LOWER(uc.content_text), nums.n, 1)
END
ORDER BY nums.n
SEPARATOR ''
) AS converted_text
FROM user_content uc
JOIN nums ON nums.n <= CHAR_LENGTH(uc.content_text)
GROUP BY uc.content_id, uc.content_text;