Split a string into rows using pure SQLite

前端 未结 4 1446
深忆病人
深忆病人 2020-12-16 06:08

Using SQLite, I\'d like to split a string in the following way.

Input string:

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

and have

4条回答
  •  情深已故
    2020-12-16 06:31

    Inspired from Lukasz Szozda's answer:

     WITH RECURSIVE cte("pre","post") AS (   
       VALUES('C:', 'Users\fidel\Desktop\Temp' || '\')
     UNION ALL
       SELECT "pre" || '\' || left("post",     position('\' in "post")-1),
                         substring("post" from position('\' in "post")+1)
       FROM cte   
       WHERE "post" > ''                             
     ) 
     SELECT "pre" FROM cte
    

    (tested on PostgreSQL)

    The idea is now to replace the VALUES line

    VALUES('C:', 'Users\fidel\Desktop\Temp' || '\')
    

    with placeholders like

     VALUES(?, ? || '\')
    

    which have been pre-split in the programming language that is going to run the SQL statement above against the data base.

    Reading the SQLite docs, I see that substring(... from ...) has to be replaced by substr(..., ...) and position(... in ...) is to be replaced by instr(..., ...) with parameters swapped.

    Very annoying for me since I wanted SQL code that runs on both PostgreSQL and SQLite.

提交回复
热议问题