mySQL - help parsing column data

Hi..
I have a comments field with Error codes and messages. The data looks like this.
"Error 1.0  Big Error   Error 2.0  Disk Error   Error 3.0 I/O  Error"
I need to parse the 3 error codes out (Some may have 2 or more)
I need to use Error X.X as the 'delimiter' So I get 3 separate records

Error 1.0  Big Error  
Error 2.0  Disk Error
Error 3.0 I/O  Error

Any ideas?
thx
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Cornelia YoderArtistCommented:
Are all the errors exactly   "Error n.0" where n is a single digit?
Are the n numbers always in order?

SInce you cannot cue on the word "Error", can you reliably cue on the dot to identify the separate fields?  How about THREE spaces that seem to separate the sections, can you reliably cue on that?
0
JElsterAuthor Commented:
No.. I could be any number after the word 'Error'
0
Cornelia YoderArtistCommented:
Will it always be a number?   Will there be any other numbers present?

How about the three spaces?

Will there be any other dots in the entire field besides the ones in the numbers?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

JElsterAuthor Commented:
Yes, no just single number like   1.0  99        123
at least 1 space after Error
Could be dots or periods in sentences.
thx
0
Cornelia YoderArtistCommented:
Will ever segment look like this?

"Error nnnn  Something Error"


Are there always THREE spaces between segments?

Do you have any control over how this string is originally formatted?

I'm trying to find a way to distinguish the "Error nnn" part from the "Something Error" part.
0
JElsterAuthor Commented:
Yes..  It would look like that

Error 1  Something Error    Error 2   Something Error       Error 25.6  Something Error

The 'delimiter' will be  'Error N'

thx
0
Cornelia YoderArtistCommented:
This should work for you:

<?php

$input = "  Error 1  Something1    Error  Error 2  Something2  Error   Error 25.6    Something3   Error  ";

//remove leading and trailing spaces and collapse all multiple spaces
trim($input);
str_replace("  "," ", $input);
str_replace("  "," ", $input);
str_replace("  "," ", $input);
str_replace("  "," ", $input);

//divide the string at the word "Error"
$array = explode("Error",$input);
$number = count($array);

//array[1] is the error number, $array[2] is the error name (without the word "Error", and so on in pairs.
//the echo statement replaces the word "Error" where it was removed by explode().
for ($i=1;$i<$number;$i=$i+2)
{
echo "Error " . $array[$i] . $array[$i+1] . "Error<br><br>";
}

//You can replace the echo statement with wherever you want the parsed lines to end up.
exit();
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
Thx.. but I don't know or have PHP.
Can it be done in SQL?
0
Cornelia YoderArtistCommented:
yikes, I doubt it, there are too many insecure formatting problems, such as variable number of spaces, double use of the word "Error", etc.  Using explode() was the only way I could think of to do it, and MySQL doesn't have an equivalent function.

Is this format something you have control over?   Wherever this comments field is created, do you have the ability to change the format/contents?
0
Cornelia YoderArtistCommented:
I'm sorry but I will have to be gone most of the day today.   I'll check back tonight when I get home.  

In the meantime, see if you can follow what I did in php:
  1.  Strip out all extra spaces
  2.  Divide the string up based on the word "Error"
  3.  Take pairs of the remaining as Number/Description pairs
  4.  Reassemble as needed
and see if you can find some MySQL functions that will do something similar.


If you have control over the format, perhaps we can simplify the entire problem by changing the formatting.
0
Ray PaseurCommented:
We are looking at one row with three different data elements in the same column.  This is a mistaken design.  The correct way to organize this data is to have three rows, each with one of the data elements.  If you find that you have to "parse" any column of a data base, it's a definite code smell that suggests a need to reorganize the data.  I think the best way forward is to reorganize now, before it becomes more difficult and expensive.  Instead of inserting multiple data elements into the same column, make two separate INSERT statements to put the data elements into separate rows.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.