Simple Recursive CTE
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

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

# Code
```mssql []
with user_table as
(
select content_id, content_text,
(upper(left(substring(content_text,1, charindex(' ',content_text+' ')-1), 1)) +
lower(substring(substring(content_text,1, charindex(' ',content_text+' ')-1), 2,  len(content_text)))) 
as first_fragmant, 
substring(content_text, charindex(' ',content_text+' ')+1, len(content_text)) as remaining_fragmant,
1 as level
from user_content

union all

select content_id, content_text,
(upper(left(substring(remaining_fragmant,1, charindex(' ',remaining_fragmant+' ')-1), 1)) +
lower(substring(substring(remaining_fragmant,1, charindex(' ',remaining_fragmant+' ')-1), 2,  len(content_text)))) as first_fragmant,
substring(remaining_fragmant, charindex(' ',remaining_fragmant+' ')+1, len(remaining_fragmant)) as remaining_fragmant,
level+1

from user_table where len(remaining_fragmant) > 0  -- also can be used: remaining_fragmant <>''
--  where charindex(' ', remaining_fragmant) > 0
)


-- Need to again apply recursion for hyphen thing. The hardcoding using case end is not a good solution. Will review the code again once I am back

select content_id, content_text as original_text, string_agg(first_fragmant_modified,' ') WITHIN GROUP (ORDER BY level) as converted_text from
(
select *, 
case when first_fragmant like '%-%' then 
substring(first_fragmant, 1, charindex('-',first_fragmant)) +
upper(substring(first_fragmant, charindex('-', first_fragmant)+1, 1)) +
substring(first_fragmant, charindex('-', first_fragmant)+2 , len(first_fragmant))
else first_fragmant
end as first_fragmant_modified
from user_table
 )x
 group by  content_id, content_text
 order by content_id



```
['MS SQL Server']