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 > Trim spaces in the middle of a string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-27-07, 08:23
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
Trim spaces in the middle of a string

hi
is there any function in mysql that trim spaces in the middle of a string?
for examle

SELECT FUNCTION_NAME("my sql")
Return "mysql"
Reply With Quote
  #2 (permalink)  
Old 02-27-07, 09:18
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
From the mysql manual, string functions -

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.
Reply With Quote
  #3 (permalink)  
Old 12-02-08, 10:45
geolemon geolemon is offline
Registered User
 
Join Date: Jun 2008
Posts: 39
Any way to do this as part of a data cleansing routine?

One customer has supplied text that looks like this:
"__100 UF____________________16V_____________6555________ _____ELEC____________SM____"
Another row:
"___0.1UF______________16V____________0603________ _____X7R______________________"

Obviously, it would be much more desirable to store that as:
"100 UF 16V 6555 ELEC SM"
"0.1UF 16V 0603 X7R"

In Excel, the "trim" function removes all whitespace, converting it into single spaces, I believe.
In Access, it doesn't work that way... in fact, displaying the raw description and the TRIM(description), I actually can't even see a difference...

If it helps, I will be running this SQL as part of an VBA script that performs the data import. I'm pulling the data from Excel spreadsheets that have been pre-formatted.

EDIT: Pretend the underscore characters above are spaces!
In a frustratingly ironic twist of fate, THIS FORUM won't even let me post repeated spaces without truncating them down to one or two!
Funny.

Last edited by geolemon; 12-02-08 at 10:48.
Reply With Quote
  #4 (permalink)  
Old 12-02-08, 11:04
geolemon geolemon is offline
Registered User
 
Join Date: Jun 2008
Posts: 39
Please, please please tell me something like THIS isn't the solution:

REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RE PLACE(DBimport.description,"----", "-"),"---","-"),"--","-"),"--","-"),"--","-"),"--","-"),"--","-")

I might have gone a little overboard - but that's part of what I'm trying to exemplify- why it doesn't seem like the right fix.

And, although even this might work 99% of the time, it's pretty Rube-Goldberg and has a finite limitation (although, 384 if I did my math right, for this particular combo)...

That's NOT elegant - there must be a "right" answer... Help me if you know it!
Reply With Quote
  #5 (permalink)  
Old 12-02-08, 11:40
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,259
if you were to run an update query several times to replace, say, 2 or 3 spaces with one space.
after you have run the query around 3..6 times you should be down to a single space.. its not nice but it shoudl work

the only other alternatuve I can think of would be to preprocess the file (using say VBA/PERL/PHP/REXX or something similar) to rip out any offending spaces. the preprocessign could take place as part of the batch job that is loading the data
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 12-02-08, 11:40
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Sadly MySQL doesn't support regexp when doing a replace - a regexp would make this quite an easy task. You could easily do this in PHP or Unix/Linux if you process the file with either of these.
Reply With Quote
  #7 (permalink)  
Old 12-02-08, 16:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by geolemon
I will be running this SQL as part of an VBA script that performs the data import.
that's your answer -- collapse the spaces in VBA before importing the data into the database
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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