How to split comma-separated value in SQLite?

前端 未结 5 529

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:05

    please create this function in your sqllite and pass 2 argument first is seprator and second string.

    CREATE FUNCTION [dbo].[Split]
    (   
          @Sep char(1)
        , @S varchar(512)
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
        WITH Pieces(pn, start, stop) AS (
          SELECT 1, 1, CHARINDEX(@Sep, @S)
          UNION ALL
          SELECT pn + 1, stop + 1, CHARINDEX(@Sep, @S, stop + 1)
          FROM Pieces
          WHERE stop > 0
        )
        SELECT pn,
          SUBSTR(@S, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS S
        FROM Pieces
    )
    
    0 讨论(0)
  • 2020-12-01 17:05

    I like the answer from @user1461607 except: it seems to me the SQLite documentation warns against assuming any particular order from a SELECT, both in the general case, and in the specific case of a recursive SELECT. Here, I modified that answer to add an ordering column in a manner that I think SQLite guarantees to work.

    I also cosmetically changed the example from a comma-separated list to a path, to suggest there are cases where you really need to process things in a particular order. This example also prints out all the columns from the temporary table so it's slightly easier to see what went on. AFAICT, a CTE in SQLite does not have the usual ROWID column, so it seems like adding some ordering column yourself really is required to sleep soundly at night.

    WITH RECURSIVE split(seq, word, str) AS (
        SELECT 0, '/', 'home/ronburk/layers/branch'||'/'
        UNION ALL SELECT
            seq+1,
            substr(str, 0, instr(str, '/')),
            substr(str, instr(str, '/')+1)
        FROM split WHERE str != ''
    ) SELECT * FROM split ORDER BY split.seq ASC;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 17:29

    You can use a common table expression to split comma separated values in SQLite.

    WITH split(word, str) AS (
        -- alternatively put your query here
        -- SELECT '', category||',' FROM categories
        SELECT '', 'Auto,A,1234444'||','
        UNION ALL SELECT
        substr(str, 0, instr(str, ',')),
        substr(str, instr(str, ',')+1)
        FROM split WHERE str!=''
    ) SELECT word FROM split WHERE word!='';
    

    Output is as expected:

    Auto
    A
    1234444
    
    0 讨论(0)
  • 2020-12-01 17:32

    SQLite provide functions for this purpose, e.g. to get substring substr('your string', start_position, end_position), to get position of a specific character in a string instr('22:string', ':'), and to get length of a string length('string'). Now let see the following examples:

    select substr('22:khan', x, y);
    returns a string starting at x and ends with y;
    select substr('22:khan', 0, instr('22:khan',':'));
    returns: 22 
    select substr('22:khan', instr('22:khan',':')+1, length('22:khan'));
    returns: khan
    select substr('22:khan',instr('22:khan',':'), length('22:khan'));
    returns: :khan
    select substr('Noor,Khan', 0, instr('Noor,Khan', ','));
    returns: Noor
    select substr('Noor,Khan', instr('Noor,Khan', ',')+1, length('Noor,Khan'));
    returns: Khan
    

    for more info visit: https://www.sqlite.org/lang_corefunc.html

    0 讨论(0)
提交回复
热议问题