Is it possible to split a word into separate lines? All the examples I found were using something to refer to as a comma or something, but I would like to separate each letter from a word, eg:
from (my table):
| id | name |
|---|---|
| 1 | banana |
to: SELECT ...
| id | letter |
|---|---|
| 1 | b |
| 1 | a |
| 1 | n |
| 1 | a |
| 1 | n |
| 1 | a |
>Solution :
One option is doing it with a recursive query, using the following two steps:
- base step: get the letter in position 1
- recursive step: get nth letter, using
LEFT(RIGHT(1), n), which extracts the letter in position n.
Recursion is halted when the nth extracting element is higher than then length of the string.
WITH RECURSIVE cte AS (
SELECT id, name,
1 AS idx,
RIGHT(LEFT(name, 1),1) AS letter
FROM tab
UNION ALL
SELECT id, name,
idx + 1 AS idx,
RIGHT(LEFT(name, idx+1), 1) AS letter
FROM cte
WHERE idx < LENGTH(name)
)
SELECT id, letter FROM cte
Output:
| id | letter |
|---|---|
| 1 | b |
| 1 | a |
| 1 | n |
| 1 | a |
| 1 | n |
| 1 | a |
Check the demo here.