PostgreSQL, avoid replacements of '-' and ' '
IntuitionThe key insight is to split the text into tokens while preserving special characters (like hyphens), then capitalize the first letter of each token and
# Intuition
The key insight is to split the text into tokens while preserving special characters (like hyphens), then capitalize the first letter of each token and reassemble the text. Regular expressions can help us handle the special cases efficiently.

# Approach
1. Use a CTE to split text into tokens using regexp_matches:
   - `[^- ]+` captures regular words
   - `[- ]+` captures special characters
2. Transform each token by:
   - Capitalizing first letter (UPPER + SUBSTR)
   - Lowercasing remaining letters (LOWER + SUBSTR)
3. Reassemble text using STRING_AGG with empty delimiter
4. Return both original and transformed text

# Complexity
- Time complexity: O(n * m)
  - n = number of rows
  - m = average length of content_text
  - Each text needs to be split and transformed character by character

- Space complexity: O(n * m)
  - Need to store tokens for each text
  - STRING_AGG creates a new string of similar size to input

# Code
```postgresql []
WITH split_text AS (
    SELECT
        content_id,
        content_text,
        -- ([^- ]+) matches one or more characters that are NOT hyphens or spaces (regular words)
        -- ([- ]+) matches one or more hyphens or spaces (special characters)
        -- 'g' flag for globally
        -- [1] gets the first element of the regexp_matches array
        (regexp_matches(content_text,
                        '(([^- ]+)|([- ]+))', 'g'))[1] AS tokens
    FROM user_content
)
SELECT
    content_id,
    content_text AS original_text,
    STRING_AGG(CONCAT(UPPER(SUBSTR(tokens, 1, 1)),
                      LOWER(SUBSTR(tokens, 2))),
               '') AS converted_text
FROM split_text
GROUP BY content_id, content_text -- needed since converted_text is aggregated
ORDER BY content_id;

```
['PostgreSQL']