Split a row into rows using pure SQLite

Using SQLite, I would like to split the string as follows.

Input line:

C:\Users\fidel\Desktop\Temp

and the query returns these lines :

C:\
C:\Users\
C:\Users\fidel\
C:\Users\fidel\Desktop\
C:\Users\fidel\Desktop\Temp

In other words, I would like to split the file path into its compound paths. Is there a way to do this in pure SQLite?

+4
source share
3 answers

This is possible with a recursive generic table expression :

WITH RECURSIVE split(s, last, rest) AS (
  VALUES('', '', 'C:\Users\fidel\Desktop\Temp')
  UNION ALL
  SELECT s || substr(rest, 1, 1),
         substr(rest, 1, 1),
         substr(rest, 2)
  FROM split
  WHERE rest <> ''
)
SELECT s
FROM split
WHERE rest = ''
   OR last = '\';

(You did not ask for a reasonable way.)

+7
source

Recursive CTE:

WITH RECURSIVE cte(org, part, rest, pos) AS (
  VALUES('C:\Users\fidel\Desktop\Temp', '','C:\Users\fidel\Desktop\Temp'|| '\', 0)
  UNION ALL
  SELECT org,
         SUBSTR(org,1, pos + INSTR(rest, '\')),
         SUBSTR(rest, INSTR(rest, '\')+1),
         pos + INSTR(rest, '\')
  FROM cte
  WHERE INSTR(rest, '\') > 0                         
)
SELECT *
FROM cte
WHERE pos <> 0
ORDER BY pos; 

SqlFiddleDemo

Output:

╔═════════════════════════════╗
β•‘            part             β•‘
╠═════════════════════════════╣
β•‘ C:\                         β•‘
β•‘ C:\Users\                   β•‘
β•‘ C:\Users\fidel\             β•‘
β•‘ C:\Users\fidel\Desktop\     β•‘
β•‘ C:\Users\fidel\Desktop\Temp β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

How it works:

org  - original string does not change
part - simply `LEFT` equivalent of original string taking pos number of chars
rest - simply `RIGHT` equivalent, rest of org string
pos  - position of first `\` in the rest 

Trace:

╔══════════════════════════════╦══════════════════════════════╦════════════════════════════╦═════╗
β•‘             org              β•‘            part              β•‘           rest             β•‘ pos β•‘
╠══════════════════════════════╬══════════════════════════════╬════════════════════════════╬═════╣
β•‘ C:\Users\fidel\Desktop\Temp  β•‘ C:\                          β•‘ Users\fidel\Desktop\Temp\  β•‘   3 β•‘
β•‘ C:\Users\fidel\Desktop\Temp  β•‘ C:\Users\                    β•‘ fidel\Desktop\Temp\        β•‘   9 β•‘
β•‘ C:\Users\fidel\Desktop\Temp  β•‘ C:\Users\fidel\              β•‘ Desktop\Temp\              β•‘  15 β•‘
β•‘ C:\Users\fidel\Desktop\Temp  β•‘ C:\Users\fidel\Desktop\      β•‘ Temp\                      β•‘  23 β•‘
β•‘ C:\Users\fidel\Desktop\Temp  β•‘ C:\Users\fidel\Desktop\Temp  β•‘                            β•‘  28 β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•
+5
source

, :

WITH RECURSIVE split(content, last, rest) AS (
VALUES('', '', 'value1Β§value2Β§value3Β§value4Β§value5Β§value6Β§value7')
UNION ALL
  SELECT 

    CASE WHEN last = 'Β§' 
            THEN
                substr(rest, 1, 1)
            ELSE
                content || substr(rest, 1, 1)
    END,
     substr(rest, 1, 1),
     substr(rest, 2)
  FROM split
  WHERE rest <> ''
)
SELECT 
       REPLACE(content, 'Β§','') AS 'ValueSplit'     
FROM 
       split
WHERE 
       last = 'Β§' OR rest ='';

:

**ValueSplit**

value1
value2
value3
value4
value5
value6
value7

, .

+3

Source: https://habr.com/ru/post/1623300/


All Articles