SQLite Forum

strings separated by ";" convert to columns
Login

strings separated by ";" convert to columns

(1) By plinio (pliniosouza) on 2021-12-29 13:15:58 [link] [source]

which command in sqlite separates a string into columns?

oracle =>

select regexp_substr('2;xxxxxxx;2361295;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;', 'misref+', 1, 3) from dual

result: 2361295

SQlite =>

This command (regexp_substr) does not work on SQLite. I need it to be in columns and not rows.


  1. ^ Misreference

(2) By Gunter Hick (gunter_hick) on 2021-12-29 13:48:59 in reply to 1 [link] [source]

There is no such function in SQLite.

You could probably devise a CTE to digest a string into parts, or you could implement a table valued function to do so.

(3) By Larry Brasfield (larrybr) on 2021-12-29 14:15:10 in reply to 1 [link] [source]

In this thread can be found a few examples of Gunter's suggestion.

(4) By plinio (pliniosouza) on 2021-12-29 17:35:11 in reply to 3 [link] [source]

Thanks for the answer, but could you give me a simpler example?

(5) By plinio (pliniosouza) on 2021-12-29 17:36:24 in reply to 2 [link] [source]

Thanks for the answer, but could you give me a simpler example?

(6) By Larry Brasfield (larrybr) on 2021-12-29 17:56:08 in reply to 5 [link] [source]

There is no simpler answer, yet, that I'm aware of. The recursive CTE's in the thread I linked can be readily adapted to your problem, but some SQL crafting will be required. Or, as Gunter suggested, a "split" extension, providing a table-valued result, would be doable. But that has not been done and made known here, to my knowledge.

With luck, some other forum participant will write that SQL for you. But do not count on that.

(7) By AlexJ (CompuRoot) on 2021-12-30 06:43:01 in reply to 4 [link] [source]

You can find examples here.

There you can find all examples related to your use case, from simplest one and up to solving sudoku.

(8) By VĂ­ctor (wzrlpy) on 2021-12-30 08:36:26 in reply to 1 [source]

You may want to use the split_part(source, sep, part) function in nalgeon's text extension.

The separator is fixed text instead of a regular expression, but that should be enough for the use case you provided.

(9) By AlexJ (CompuRoot) on 2021-12-30 19:49:46 in reply to 8 [link] [source]

If it is just one time operation, then I really can't see a reason to compile own extension, it is so trivial task for any external tools, like an awk for example that available on any platforms

#!/bin/sh

### creating test database
echo "
begin transaction;
create table t ( str text );
insert into t (str) values ('2;xxxxxxx;2361295;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;');
insert into t (str) values ('2;xxxxxxx;2361296;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;');
insert into t (str) values ('2;xxxxxxx;2361297;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;');
commit;
" | sqlite3 plinio.db


### extraction of 3rd field
echo 'select str from t;' | sqlite3 plinio.db | awk -F\; '{print $3}'

(10) By Ryan Smith (cuz) on 2021-12-31 00:16:33 in reply to 1 [link] [source]

Here's one that's maybe simpler and with different versions (read the whole thread) that splits on spaces (but you can change those to commas or semicolons easily):

forum post 840c98a8e87c2207?t

It does split it into rows though. There is no way in any SQL engine (SQLite or otherwise) to split text into output columns, as you seem to be requesting. All output columns, for any query, have to be known at the time of parsing.

Hope that helps.

(11) By plinio (pliniosouza) on 2022-01-03 16:35:33 in reply to 8 [link] [source]

Thanks for the reply, but it gives the message that this function does not exist in sqlite.

select split_part(s_recado,';',2) from tp_recados

2.6 Message : no such function:_split_part

(12) By Larry Brasfield (larrybr) on 2022-01-03 17:20:16 in reply to 11 [link] [source]

Did you load the extension?

(13) By plinio (pliniosouza) on 2022-01-03 21:07:35 in reply to 12 [link] [source]

Could you tell me the correct extension?

Sqlite expert - Personal Edition Version 3.5.76.2496

(14) By Stephan Beal (stephan) on 2022-01-03 21:18:25 in reply to 13 [link] [source]

Could you tell me the correct extension?

The message you responded to when you wrote:

Thanks for the reply, but it gives the message that this function does not exist in sqlite

Has a link to the extension, its C file, and instructions for how to load it.

(15) By anonymous on 2022-01-04 15:54:11 in reply to 5 [link] [source]

Another idea: use the json1 module (built-in in sqlite cli) to create a JSON-array with some tiny string-tinkering:

  • json_quote the string
  • replace all ; with ","
  • embed in [ ]

sample data as of one of the other suggestions here:

create table t ( str text );
insert into t (str) values ('2;xxxxxxx;2361295;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;');
insert into t (str) values ('2;xxxxxxx;2361296;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;');
insert into t (str) values ('2;xxxxxxx;2361297;2775371;345640;2638;yyyyyyyyy;1084;fffffff 1;28/12/2021;28/12/2021;');

split all

> select key, value
> from t, json_each( '['||replace(json_quote(str),';','","')||']' );

0|2
1|xxxxxxx
2|2361295
3|2775371
4|345640
5|2638
6|yyyyyyyyy
...

or just each 3rd entry

> select value
> from t, json_each( '['||replace(json_quote(str),';','","')||']' )
> where key=2;

2361295
2361296
2361297

Should work with other separators too by changing the replace(...,';',...) accordingly. And with fairly pathetic strings containing quotes and more as well thanks to the json_quote.