Writing a pure SQL based String Tokenizer
A quite common concept in many programming langiages, though perhaps strongest in Java, is the String Tokenizer. This is a utility class or function that takes a String as input, and optionally one or more delimiters, and breaks up the string in individual elements, returning those as an appropriate collection. As an example: a String Tokenizer could split the string "red,blue,yellow" into a collection of the strings red, blue and yellow – if the comma had been specified as delimiter or no delimiter had been defined and the comma was the default. With the "e" as delimiter, the String Tokenizer would have produced: "r", "d,blu",",y" and "llow".
PL/SQL has something akin to the String Tokenizer, in the DBMS_UTILITY.COMMA_TO_TABLE function. By the way, my colleague Alex discussed this function in quite a bit of detail in his recent article Never Assume, Always Test.
While preparing for our session "SQL Puzzles" (next Tuesday, 13th February) I needed a String Tokenizer in SQL, ideally nothing but SQL. Again, with a different objective in mind, I wrote a SQL Query that indeed acts as a Tokenizer. I am quite sure it can be done much better – Anton: please share with me your thoughts on this – but I will share my attempt with you anyway.
The query is this one:
with numbers as<br />( select rownum rn<br /> from ( select count(*) rn<br /> from dual<br /> group<br /> by cube(1,2,3,4)<br /> )<br />)<br />, input as<br />( select 'red,blue,yellow' string_to_tokenize<br /> , 'e' delimiter<br /> from dual<br />)<br />, positions as<br /> ( select rn pos<br /> from numbers<br /> join<br /> input<br /> on instr(string_to_tokenize, delimiter, 1, rn) > 0<br /> union<br /> select 0 pos<br /> from input<br /> where instr(string_to_tokenize, delimiter) > 0<br />)<br />select substr( string_to_tokenize<br /> , 1 + case pos when 0 then 0 else instr(string_to_tokenize, delimiter, 1, pos) end<br /> , case instr(string_to_tokenize, delimiter, 1, pos+1) when 0 then length(string_to_tokenize)+2 else instr(string_to_tokenize, delimiter, 1, pos+1)+1 end<br /> - ( 2 + case pos when 0 then 0 else instr(string_to_tokenize, delimiter, 1, pos) end)) token<br />from positions<br />, input<br />
When I run the query on , the results are:
TOKEN<br />------------------<br />red<br />blue<br />yellow <br />
And when I use "e" as the delimiter, the results are:
TOKEN<br />-------------<br />r<br />d,blu<br />,y<br />llow<br />
Note that the inline-view numbers is somewhat limited as right now it will return no more than 16 rows. There are of course several ways to increase or tune that number, one would be to have numbers adapt to the inline-view input, that should probably be defined first in the query.
Also note that this query could be run using SQL*Plus parameters, like this:
SQL> with numbers as<br /> 2 ( select rownum rn<br /> 3 from ( select count(*) rn<br /> 4 from dual<br /> 5 group<br /> 6 by cube(1,2,3,4)<br /> 7 )<br /> 8 )<br /> 9 , input as<br /> 10 ( select '&String_To_Tokenizer' string_to_tokenize<br /> 11 , '&Delimiter' delimiter<br /> 12 from dual<br /> 13 )<br /> 14 , positions as<br /> 15 ( select rn pos<br /> 16 from numbers<br /> 17 join<br /> 18 input<br /> 19 on instr(string_to_tokenize, delimiter, 1, rn) > 0<br /> 20 union<br /> 21 select 0 pos<br /> 22 from input<br /> 23 where instr(string_to_tokenize, delimiter) > 0<br /> 24 )<br /> 25 select substr( string_to_tokenize<br /> 26 , 1 + case pos when 0 then 0 else instr(string_to_tokenize, delimiter, 1, pos) end<br /> 27 , case instr(string_to_tokenize, delimiter, 1, pos+1) when 0 then length(string_to_toke<br />nize)+2 else instr(string_to_tokenize, delimiter, 1, pos+1)+1 end<br /> 28 - ( 2 + case pos when 0 then 0 else instr(string_to_tokenize, delimiter, 1, pos) end)<br />) token<br /> 29 from positions<br /> 30 , input<br /> 31 /<br />Enter value for string_to_tokenizer: in the land of the pigs, the butcher is king (by Meat Loaf)<br />old 10: ( select '&String_To_Tokenizer' string_to_tokenize<br />new 10: ( select 'in the land of the pigs, the butcher is king (by Meat Loaf)' string_to_tokenize<br />Enter value for delimiter: <br />old 11: , '&Delimiter' delimiter<br />new 11: , ' ' delimiter<br /><br />TOKEN<br />--------------------------------------------------------------------------------<br />in<br />the<br />land<br />of<br />the<br />pigs,<br />the<br />butcher<br />is<br />king<br />(by<br /><br />TOKEN<br />--------------------------------------------------------------------------------<br />Meat<br />Loaf)<br /><br />13 rows selected. <br />
Finally, the query should be extended to support multiple delimiters.
SELECT SUBSTR(X,Y + 1, Z – 1 – Y)
FROM (
SELECT X, INSTR(X,DELIMITER,1,ROWNUM) Y, INSTR(X,DELIMITER,1,ROWNUM+1) Z
FROM (SELECT ‘,’ || ‘A,BB,CC’ || ‘,’ X, ‘,’ DELIMITER FROM DUAL), ALL_TABLES
WHERE INSTR(X,DELIMITER,1,ROWNUM+1) != 0
)