MS SQL Server Solution (easy readable SQL solution) [Time complexity: O(Nlogn)]
IntuitionI feel like I have split the content into words and then use upper and lower method to change the capitalization, then I have to concat and aggregate t
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
I feel like I have split the content into words and then use upper and lower method to change the capitalization, then I have to concat and aggregate the string to get the converted text, then we can join the result with the user_content table again to get the original text.

![image.png](https://assets.leetcode.com/users/images/9d224ba5-48a8-4e9d-a5f3-fd71c5b0dcdc_1739202163.391332.png)

# Approach
<!-- Describe your approach to solving the problem. -->
1. Create a cte with content_id and words managing the change of the capitalization for words seperated by space and hyphen replaced by space hyphen space to make it easy for us to change the capitalization.
```
WITH user_content_split AS (
    SELECT content_id, CONCAT(UPPER(SUBSTRING(value, 1, 1)), LOWER(SUBSTRING(value, 2, LEN(value)))) AS 'words'
    FROM user_content
    CROSS APPLY STRING_SPLIT(REPLACE(content_text, '-', ' - '), ' ')
)
```
2. return the space hyphen space to hyphen and aggregate the strings to get the converted text, then join the table with the user_content to get the original text, order by content_id (I don't know why the order changes).
```
SELECT ucs.content_id AS 'content_id', content_text AS 'original_text', REPLACE(STRING_AGG (words, ' '),' - ', '-') AS 'converted_text'
FROM user_content_split ucs JOIN user_content uc ON ucs.content_id = uc.content_id
GROUP BY ucs.content_id, content_text
ORDER BY content_id 
```

# Complexity
- Time complexity: O(Nlogn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

# Code
```mssql []
/* Write your T-SQL query statement below*/
WITH user_content_split AS (
    SELECT content_id, CONCAT(UPPER(SUBSTRING(value, 1, 1)), LOWER(SUBSTRING(value, 2, LEN(value)))) AS 'words'
    FROM user_content
    CROSS APPLY STRING_SPLIT(REPLACE(content_text, '-', ' - '), ' ')
)



SELECT ucs.content_id AS 'content_id', content_text AS 'original_text', REPLACE(STRING_AGG (words, ' '),' - ', '-') AS 'converted_text'
FROM user_content_split ucs JOIN user_content uc ON ucs.content_id = uc.content_id
GROUP BY ucs.content_id, content_text
ORDER BY content_id 

```
['String', 'Database', 'MS SQL Server']