473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pulling a number out of a mySQL text field?

LRW
I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:

$sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking`
FIELDS TERMINATED BY ','".
"OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'".
"( `tr_trackno` , `tr_ordernum` , `tr_shipname` , `tr_shipaddr` ,
`tr_citystate` , `tr_zip` , `tr_weight` , `tr_method` , `tr_shipdate`
)";

Now, one of the columns is supposed to be a 5-digit integer
column...but the person who generates the spreadsheet (a 3rd party I
have no control over) ocassionally has other text in that column in
some records. Say 3 out of 50.

For example, what it's supposed to have is data like: '12345', but
instead it sometimes has: '12345 Some Extra Text Here', and sometimes
there will be a doublequoute in front: '"12345 Some Extra Text'.

No my question is, is there a way to pull a 5-digit number out of a
field? If I could be gaurenteed that the number would always be the
1st 5 digits, I could just do a len() and then crop everything after
the 5th character. But when there's ocassionally a character in front
of the number, that won't work.

What if I make the database column a 5-digit init field? Would it
automatically only insert the 5-digit part of the field? (Doubt it.)

Thanks for any suggestions!!
Liam
Jul 17 '05 #1
6 3347
Nel
"LRW" <de**@celticbear.com> wrote in message
news:3a**************************@posting.google.c om...
I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:

$sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking`
FIELDS TERMINATED BY ','".
"OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'".
"( `tr_trackno` , `tr_ordernum` , `tr_shipname` , `tr_shipaddr` ,
`tr_citystate` , `tr_zip` , `tr_weight` , `tr_method` , `tr_shipdate`
)";

Now, one of the columns is supposed to be a 5-digit integer
column...but the person who generates the spreadsheet (a 3rd party I
have no control over) ocassionally has other text in that column in
some records. Say 3 out of 50.

For example, what it's supposed to have is data like: '12345', but
instead it sometimes has: '12345 Some Extra Text Here', and sometimes
there will be a doublequoute in front: '"12345 Some Extra Text'.

No my question is, is there a way to pull a 5-digit number out of a
field? If I could be gaurenteed that the number would always be the
1st 5 digits, I could just do a len() and then crop everything after
the 5th character. But when there's ocassionally a character in front
of the number, that won't work.

What if I make the database column a 5-digit init field? Would it
automatically only insert the 5-digit part of the field? (Doubt it.)

Thanks for any suggestions!!
Liam


If I were you I would split the first colum up into two fields
e.g. number - INTEGER
comments - TEXT

Check form input for the number field and make sure it's always a number.
If not print error and return user to form, else add to database. If needs
be you could also check to ensure the input number is 5 digits long.

IMHO best to start right than trying to bodge through dirty data.

Nel.
Jul 17 '05 #2
LRW wrote:
I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:

$sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking`
FIELDS TERMINATED BY ','".
"OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'".
"( `tr_trackno` , `tr_ordernum` , `tr_shipname` , `tr_shipaddr` ,
`tr_citystate` , `tr_zip` , `tr_weight` , `tr_method` , `tr_shipdate`
)";

Now, one of the columns is supposed to be a 5-digit integer
column...but the person who generates the spreadsheet (a 3rd party I
have no control over) ocassionally has other text in that column in
some records. Say 3 out of 50.

For example, what it's supposed to have is data like: '12345', but
instead it sometimes has: '12345 Some Extra Text Here', and sometimes
there will be a doublequoute in front: '"12345 Some Extra Text'.

No my question is, is there a way to pull a 5-digit number out of a
field? If I could be gaurenteed that the number would always be the
1st 5 digits, I could just do a len() and then crop everything after
the 5th character. But when there's ocassionally a character in front
of the number, that won't work.

What if I make the database column a 5-digit init field? Would it
automatically only insert the 5-digit part of the field? (Doubt it.)

Thanks for any suggestions!!
Liam


yes, but you will not be able to use LOAD DATAFILE to do it.. you will basically
need to "roll-your-own".

you should be getting errors on those fields if the column data type is number
or similar. What I would do is run the normal load, then have a seperate
"alternate" process that reads the file, determines any abnormal occurances of
text in a number field and then use a regexp to change the record and load it.
The load feature does not appear to write an exceptions file for those records
that might fail - at least not that I have seen.... maybe this should be an
enhancement request for later versions...

--
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #3
"LRW" wrote:
I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:

$sql = "LOAD DATA INFILE ’".$uploadfile."’ INTO TABLE
`tbl_tracking`
FIELDS TERMINATED BY ’,’".
"OPTIONALLY ENCLOSED BY ’\"’ LINES TERMINATED BY
’\n’".
"( `tr_trackno` , `tr_ordernum` , `tr_shipname` , `tr_shipaddr` ,
`tr_citystate` , `tr_zip` , `tr_weight` , `tr_method` , `tr_shipdate` )";

Now, one of the columns is supposed to be a 5-digit integer
column...but the person who generates the spreadsheet (a 3rd party I have no control over) ocassionally has other text in that column in
some records. Say 3 out of 50.

For example, what it’s supposed to have is data like:
’12345’, but
instead it sometimes has: ’12345 Some Extra Text Here’,
and sometimes
there will be a doublequoute in front: ’"12345 Some Extra
Text’.

No my question is, is there a way to pull a 5-digit number out of a
field? If I could be gaurenteed that the number would always be the
1st 5 digits, I could just do a len() and then crop everything after the 5th character. But when there’s ocassionally a character in
front
of the number, that won’t work.

What if I make the database column a 5-digit init field? Would it
automatically only insert the 5-digit part of the field? (Doubt it.)
Thanks for any suggestions!!
Liam


read the free-form text field
let’s call it
$freeform = "abcz23557 xyz";
preg_match("/\D\d{5}\D/", $freeform, $Arr);
array $Arr[1] would be the first 5 digit integer, $Arr[2] would be the
2nd one and so forth.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-pulling-...ict137884.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=460750
Jul 17 '05 #4
Nel wrote:
"LRW" <de**@celticbear.com> wrote in message
news:3a**************************@posting.google.c om...
I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:

$sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking`
FIELDS TERMINATED BY ','".
"OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'".
"( `tr_trackno` , `tr_ordernum` , `tr_shipname` , `tr_shipaddr` ,
`tr_citystate` , `tr_zip` , `tr_weight` , `tr_method` , `tr_shipdate`
)";

Now, one of the columns is supposed to be a 5-digit integer
column...but the person who generates the spreadsheet (a 3rd party I
have no control over) ocassionally has other text in that column in
some records. Say 3 out of 50.

For example, what it's supposed to have is data like: '12345', but
instead it sometimes has: '12345 Some Extra Text Here', and sometimes
there will be a doublequoute in front: '"12345 Some Extra Text'.

No my question is, is there a way to pull a 5-digit number out of a
field? If I could be gaurenteed that the number would always be the
1st 5 digits, I could just do a len() and then crop everything after
the 5th character. But when there's ocassionally a character in front
of the number, that won't work.

What if I make the database column a 5-digit init field? Would it
automatically only insert the 5-digit part of the field? (Doubt it.)

Thanks for any suggestions!!
Liam

If I were you I would split the first colum up into two fields
e.g. number - INTEGER
comments - TEXT

Check form input for the number field and make sure it's always a number.
If not print error and return user to form, else add to database. If needs
be you could also check to ensure the input number is 5 digits long.

IMHO best to start right than trying to bodge through dirty data.

Nel.

Nel,

FYI, the OP said the source is a third-party that sends a file so there is no
"form" to check. :)
--
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #5
Nel
> Nel,

FYI, the OP said the source is a third-party that sends a file so there is no "form" to check. :)
--
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)


Did I mention I can't read? :-)
Jul 17 '05 #6
de**@celticbear.com (LRW) wrote in message
news:<3a**************************@posting.google. com>...

I have an automated process which uploads a comma separated
spreadsheet (csv) and inserts it into a database:

$sql = "LOAD DATA INFILE '".$uploadfile."' INTO TABLE `tbl_tracking`
FIELDS TERMINATED BY ','".
"OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'".
"( `tr_trackno` , `tr_ordernum` , `tr_shipname` , `tr_shipaddr` ,
`tr_citystate` , `tr_zip` , `tr_weight` , `tr_method` , `tr_shipdate`
)";

Now, one of the columns is supposed to be a 5-digit integer
column...but the person who generates the spreadsheet (a 3rd party I
have no control over) ocassionally has other text in that column in
some records. Say 3 out of 50.

For example, what it's supposed to have is data like: '12345', but
instead it sometimes has: '12345 Some Extra Text Here', and sometimes
there will be a doublequoute in front: '"12345 Some Extra Text'.

No my question is, is there a way to pull a 5-digit number out of a
field?


Yes:

$data = '"12345 Some Extra Text';
$data = str_replace('"', '', $data);
list($data,) = explode(' ', $data);

Cheers,
NC
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jason | last post by:
I have a form which ads a technician service ticket into mysql. The fields are tech name time spent service client name etc... I assumed that mysql was storing the date of each record...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
2
by: snowweb | last post by:
I've created a web page in .php which receives it's content from a MySQL database, it is all working nicely and looking good except that a field which I have called Ed_Main is now about 65kb and now...
0
by: NewbieSupreme | last post by:
I'm using PHPMyAdmin on an Apache2Triad install (latest version; 5.x.x, which installs PHP5 and PHPMyAdmin 2.8 as well). In Access, I exported a table to a text file, tab-delimited, text qualifyer...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
2
by: S.Dickson | last post by:
I have just moved over to mysql as the back end and keeping access as front end. My database is for a ordering system. I use autonumber as the Order Number. as mysql does not display the order...
5
by: alf8kitty | last post by:
Hello, I have a form that allows a user to submit notes in a textarea. <td><textarea name="Notes" value="" ROWS=3 COLS=35></textarea></td> The notes get sent to the MySQL database correctly...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
1
by: ephemie | last post by:
Hi all, Not sure whether this is a javascript or MySql issue. Have below javascript code of embedded flash player stored in text field in MySql, with php reading it into a variable and echoing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.