Splitting Comma-Separated Values with SQLite's Common Table Expressions
Question:
How can I effortlessly split a comma-separated string in the Category column of a SQLite table? I seek a simpler approach than using Replace() and Trim() and avoid the limitations of substr().
Answer:
SQLite offers a feature called Common Table Expressions (CTEs) that allows for recursive queries, making it convenient to split comma-separated values. Here's a breakdown:
Query:
WITH split(word, csv) AS ( SELECT '', 'Auto,A,1234444'||',' UNION ALL SELECT substr(csv, 0, instr(csv, ',')), substr(csv, instr(csv, ',') + 1) FROM split WHERE csv != '' ) SELECT word FROM split WHERE word!='';
Explanation:
Output:
Auto A 1234444
The above is the detailed content of How to Efficiently Split Comma-Separated Strings in SQLite Using CTEs?. For more information, please follow other related articles on the PHP Chinese website!