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
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3