473,608 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3358
Nel
"LRW" <de**@celticbea r.com> wrote in message
news:3a******** *************** ***@posting.goo gle.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? 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**@celticbea r.com> wrote in message
news:3a******** *************** ***@posting.goo gle.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...bu t 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
automatical ly 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.go ogle.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
532
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 added to the
3
14118
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 (and store it in field 2). > > I would like to run a query that returns all the data in the table plus the > record number, in a similar sense to the getuser() command to get the User's > Identity, I would like a GetRecord() command.
2
5321
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 does not display all the text stored in it. I'm using PHPMyadmin to administer MySQL. MySQL version 4.0 (CGI setup) MySQL installed on local machine (high-end Pentium laptop) IIS installed on local machine OS - XP Pro SP2 Browser: IE6 ...
0
3466
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 of "none" (this is how I read to do it from newsgroups). When I use the Query window in phpmyadmin to import the text file, it waits a while, then returns an error: #1064 - You have an error in your SQL syntax; check the manual that...
3
3285
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 the field should only be showing one or the other (date or time). Even on the back end of the database where the column properties are, I have chosen the smallest date/time formats. When the aspx page runs, it shows the date and time (ie:in a...
2
7345
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 number (autonumber) until the form has been saved. Is there a way on the order form that i can have a button that will save the data then reopen the form so the order number ( autonumber is displayed) I am currently only learning about access...
5
2290
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 when I check out the database entries. I have another form on the same page that pulls the notes field (along with some others) to display in another textarea. However, although the other fields pulled are displayed, the notes textarea remains...
12
2363
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 registry mark given by underwriters (as at Lloyd's) to ships in first-class condition. Inferior grades are indicated by A 2 and A 3. () The first three letters of the alphabet, used for the whole alphabet. () In church or chapel style; -- said of...
1
1482
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 it, however no player shows. Code is seen when a view source of page is done, so it's reading it out ok. Am I missing escape characters or something else completely? Code runs ok in a normal html page. thx in advance
0
8011
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8503
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8488
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8358
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6826
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5482
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3972
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.