Home » SQL & PL/SQL » SQL & PL/SQL » Split column data (Oracle 10)
Split column data [message #606500] Fri, 24 January 2014 23:51 Go to next message
vs_net
Messages: 1
Registered: January 2014
Junior Member
Hi,
I have a requirement where i need to split the value of a table col varchar2(100) into 4 different fields. I would need to use a view (along with a function) to query the field and then put the values in the 4 columns. The only catch here is that none of the words should be split up i.e incase there is a word at the end of the column it should go in the next column as such. Basically i am looking for word wrapping. Can someone please let me know how to go about this

Eg:-
Suppose i have a col. of length 30 and need to split the value in 3 columns
123456789012345678901234567890
Hi I need to do a word wrap of
Expected result (pl. note that 'word' has moved to col3 since it is btn position 19-23)

col1 = Hi I need
Col2 = to do a
col3 = word wrap o

[EDITED by LF: applied [pre] tags to preserve formatting]

[Updated on: Sat, 24 May 2014 03:30] by Moderator

Report message to a moderator

Re: Split column data [message #606501 is a reply to message #606500] Sat, 25 January 2014 00:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Split column data [message #606505 is a reply to message #606500] Sat, 25 January 2014 00:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Suppose i have a col. of length 30 and need to split the value in 3 columns


And the rules to split are (apart do not split a word)?
Note that from what you said a valid solution is:
col1="Hi I need to do a word wrap of"
col2=null
col3=null
No words are split (lone rule you gave).

Re: Split column data [message #606519 is a reply to message #606505] Sat, 25 January 2014 09:57 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
I guess there are three issues here:

1. what are the rules for splitting the field?
2. what is the code the does #1?
3. how does one return four different fields in a query using a splitter function as is implied by #1,#2?

To whit:

1. only you know the rules

2. we don't normally write code for people, only point them in the right direction, though if someone wants to provide you some code that is OK

3. there are two easy ways to split one field into multiple columns in a query:
   a. use a function of complex expression called four times, once for each field to be returned
   b. use a user defined object and table types and return same from a table function in order to present the fields as defined by the object types

Here is an example of the type thing.

select a.*,b.*
from sometable a
    ,table(cast(tablefunction(a.somecolumn) as userdefinedtype)) b
/

You will see a PICKLER FETCH I believe in the query plan. For each row from <sometable> the table function will be called to which will be passed the column <somecolumn>. Oracle has various efficiencies for this depending upon your database version. You may also wish to look into PIPELINED TABLE FUNCTION if the number of rows from <sometable> will be large (> 1000?).

I leave it to you to read about user defined functions and object types in order to work out the details of defining an object type and a table type and then writing a function that returns the table type.

good luck. Kevin
Re: Split column data [message #614624 is a reply to message #606500] Fri, 23 May 2014 22:55 Go to previous message
Barbara Boehmer
Messages: 9088
Registered: November 2002
Location: California, USA
Senior Member
If you are just looking for word wrapping, then SQL*Plus can do that for you, as shown below.

SCOTT@orcl12c> create table test_tab (test_col varchar2(30))
2 /

Table created.

SCOTT@orcl12c> insert into test_tab (test_col) values
2 ('Hi I need to do a word wrap of')
3 /

1 row created.

SCOTT@orcl12c> select * from test_tab
2 /

TEST_COL
------------------------------
Hi I need to do a word wrap of

1 row selected.

SCOTT@orcl12c> column test_col format a12 word_wrapped
SCOTT@orcl12c> select * from test_tab
2 /

TEST_COL
------------
Hi I need to
do a word
wrap of


1 row selected.
Previous Topic: Function that will create comma separated list
Next Topic: rejected row capture
Goto Forum:
  


Current Time: Fri Apr 19 22:19:32 CDT 2024