How to split comma-separated value in SQLite?

前端 未结 5 531

I want to split comma-separated String inside SQLite database

Example: I have a Category column in 1 of my table.

|Category         


        
5条回答
  •  情深已故
    2020-12-01 17:23

    This variation of the answer provided by @user1461607 ensures that any CSV values that happen to be empty strings are preserved:

    WITH RECURSIVE split(value, str) AS (
          SELECT null, 'Auto,A,1234444' || ','  -- the string to be split 
          UNION ALL
          SELECT
          substr(str, 0, instr(str, ',')),
          substr(str, instr(str, ',')+1)
          FROM split WHERE str!=''
      ) SELECT value FROM split WHERE value is not NULL;
    

    Converting a CSV line to a JSON array

    Assuming the JSON1 extension has been loaded, you could use json_group_array(value) in the last line to convert the CSV to a JSON array of strings.

提交回复
热议问题