SQL Solution: CTE with String Split
IntuitionThe problem requires transforming the text in column content_text with the following rules:
Convert the first letter of each word to uppercase, keeping
# Intuition
The problem requires transforming the text in column `content_text` with the following rules:

1. Convert the first letter of each word to uppercase, keeping the others in lowercase.
2. For words connected by a hyphen `-`, each part must have the first letter capitalized.
3. Preserve all existing format, spacing and exsitng punctuation. 

# Approach
## 1. Separate words
- Using `STRING_SPLIT` to separate the text in words with a separator, including the hyphen transforming to guarantee the correct treatment to these words.
- Using `REPLACE` to temporary replacing the hyphen `-` to `' - '`, for separate them an individual words without losing the context. 
## 2. Transform words
- Applying the `UPPER` to the first letter and `LOWER` to the rest of each other word.
- Using again the `REPLACE` to revert the hyphen transforms, concating the connected parts.
## 3. Concat text
- With `STRING_AGG`, join the formatted words to a unique row, preserving the spacing.
## 4. Final result
- Return the table with all text capitalized.

# Complexity
- Time complexity: $$O(N\ log N)$$

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```mssql []
WITH CTE AS (
SELECT content_id,
    content_text,
    -- Uppercasing the first letter
    CONCAT(UPPER(LEFT(value,1)), LOWER(SUBSTRING(value, 2, LEN(value)))) words

FROM user_content
-- Setting string split and adding more spacing in the hyphen
CROSS APPLY STRING_SPLIT(REPLACE(content_text, '-', ' - '), ' ')
)
SELECT content_id,
    content_text original_text,
    -- Reverting the hyphen treatment
    REPLACE(STRING_AGG(words, ' '), ' - ', '-') converted_text

FROM CTE
GROUP BY content_id, content_text
ORDER BY content_id
```
['MS SQL Server']