Wish Oracle had an inbuilt function "SPLIT" [message #590886] |
Tue, 23 July 2013 13:44 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Hi folks,
I wish Oracle comes up with an inbuilt function to split the string depending on the delimiter we pass as parameters.
Somewhat like the str2tbl function of Tom Kyte in his example -
CREATE OR REPLACE TYPE mytabletype AS TABLE OF VARCHAR2 (255);
CREATE OR REPLACE FUNCTION str2tbl (
p_str IN VARCHAR2,
p_delim IN VARCHAR2 DEFAULT '.'
)
RETURN mytabletype
AS
l_str LONG DEFAULT p_str || p_delim;
l_n NUMBER;
l_data mytabletype := mytabletype ();
BEGIN
LOOP
l_n := INSTR (l_str, p_delim);
EXIT WHEN (NVL (l_n, 0) = 0);
l_data.EXTEND;
l_data (l_data.COUNT) := LTRIM (RTRIM (SUBSTR (l_str, 1, l_n - 1)));
l_str := SUBSTR (l_str, l_n + LENGTH (p_delim));
END LOOP;
RETURN l_data;
END;
/
I have used the above Tom Kyte's code a lot of times shamelessly.
I agree that with the advent of regular expressions it has made the coders life much easier and REGEXPs works wonderfully, but, the workaround to achieve the result is sometimes more than what we expect initially while coding.
[Updated on: Tue, 23 July 2013 13:54] Report message to a moderator
|
|
|
|
Re: Wish Oracle had an inbuilt function "SPLIT" [message #590894 is a reply to message #590893] |
Tue, 23 July 2013 14:32 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Blackswan-
normal forms are OK. I am thinking about projects/teams that work on raw data.
What about the data like IP address, where each field separated by a delimiter has a meaning. And it gets stored as raw data in a column. While fetching/processing we need to split them.
So, per the 3rd normal form, I cant create a look up table for IP adrreses of billion users globally.
I still think it is very useful.
[Updated on: Tue, 23 July 2013 14:39] Report message to a moderator
|
|
|
|
|
|
Re: Wish Oracle had an inbuilt function "SPLIT" [message #590908 is a reply to message #590902] |
Tue, 23 July 2013 19:21 |
|
BlackSwan
Messages: 26766 Registered: January 2009 Location: SoCal
|
Senior Member |
|
|
I share with you the basic law about holes.
When you find yourself in a hole, the first thing you should do is Stop Digging!
If you never place multiple values delimited by separator character into a single column; then there is NO need for any SPLIT function.
|
|
|