If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Split one Field into several fields

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-09-11, 09:04
jagr2510 jagr2510 is offline
Registered User
 
Join Date: May 2011
Posts: 1
Split one Field into several fields

Hello,

here a part of the table:

id = 0000000006
actiontype = 1
datafield = 90000,Manuel

i wrote following script, to split datafield values in two fields:

Select `actiontype`,
case when instr(`datafield`, ',') > 0
then substr(`datafield`, 1, instr(`datafield`, ',')-1)
else ' '
end as user_id
,
case when instr(`datafield`, ',') > 0 and instr(`datafield`, ',') < length(`datafield`)
then substr(`datafield`, instr(`datafield`, ',') + 1)
else `datafield`
end as first_name
from `interfacetbl`
WHERE actiontype = 1

Now, i would like to know how to split field values in more then two fields,
for example:


id = 0000000006
actiontype = 1
datafield = 90000,Manuel,Weis,printcarrier.com

The four fields:

user_id = 90000
first_name = Manuel
last_name = Weis
company = printcarrier.com

How to realize?

Last edited by jagr2510; 05-09-11 at 09:21.
Reply With Quote
  #2 (permalink)  
Old 05-09-11, 11:56
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi, it should be mentioned that if you need to perform this type of operation then you should look more closely at your data model. As an exercise I have put together some code and explanation on how this can be done here
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On