473,473 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Whitespace in varchar field

110 New Member
I've just realized that one of my varchar fields which holds musical genres is full of whitespace.

I've looked online and found a few references saying that text fields have whitespace but that varchar fields will strip the whitespace, but this does not seem to be the case.

Strangely, my output looks like the genres are centered in the field (or maybe that is just how it display on output from command line mysql.
Here is what the output looks like (edit- for some reason the centering effect I get in my output gets cleaned up when I submit to the forum).
Is there any way to clean this up?

Expand|Select|Wrap|Line Numbers
  1.  
  2. mysql> SELECT genre FROM artists WHERE genre LIKE '%Rock%' LIMIT 0,5;
  3. +-----------------------------------------------------------+
  4. | genre                                                     |
  5. +-----------------------------------------------------------+
  6. |                                       2-step / Acousmatic / Tape music / Classic Rock                                  |
  7. |                                       2-step / Acousmatic / Tape music / Rock                                          |
  8. |                                       2-step / Acoustic / Rock                                                        |
  9. |                                       2-step / Alternative / Classic Rock                                          |
  10. |                                       2-step / Alternative / Classic Rock                                          |
  11. +-----------------------------------------------------------+
  12. 5 rows in set (0.04 sec)
  13.  
Sep 23 '08 #1
3 7407
Atli
5,058 Recognized Expert Expert
From the manual
For VARCHAR columns, excess trailing spaces are truncated prior to insertion and a warning is generated, regardless of the SQL mode in use. For CHAR columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode.

VARCHAR values are not padded when they are stored. Handling of trailing spaces is version-dependent. As of MySQL 5.0.3, trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL. Before MySQL 5.0.3, trailing spaces are removed from values when they are stored into a VARCHAR column; this means that the spaces also are absent from retrieved values.
By "excess trailing spaces" they mean spaces that exceed the given maximum length of the field.

To clean that up, you could use the TRIM() function.
Sep 23 '08 #2
pedalpete
110 New Member
Thanks Atli,

I didn't realize mysql had a trim function and i was hoping to not do this through PHP.
Unfortunately, the trim function doesn't seem to be working for me.

I've tried both of the following, but still get my response back with spaces.
Expand|Select|Wrap|Line Numbers
  1. SELECT CONCAT(  '/ ', TRIM(  '               ' FROM genre ) ,  ' /' ) 
  2. FROM artists
  3. WHERE genre LIKE  '%rock%'
  4. LIMIT 0 , 10
  5.  
or
Expand|Select|Wrap|Line Numbers
  1. SELECT CONCAT(  '/ ', TRIM( genre ) ,  ' /' ) 
  2. FROM artists
  3. WHERE genre LIKE  '%rock%'
  4. LIMIT 0 , 10
  5.  
With both of these, i still have about 7 or 8 spaces on each side of my genre.
I've tried with the "BOTH"
Sep 23 '08 #3
coolsti
310 Contributor
Are you sure that you are actually getting the whitespace from your database? Is it possible that the whitespace is appearing somehow in whatever is presenting the results to you?

Because it looks as if you are doing everything correctly.

Try to do a query where you just spit out the raw genre field as TRIM(genre), perhaps with a non-whitespace symbol concatenated on both sides, for example CONCAT('X',TRIM(genre),'X') to see if the problem is with your query or with your presentation. In the above, do not add any spaces at all, as you do in your query, just to help debug this.
Sep 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bill | last post by:
In an effort to improve the speed of queries against my main table, I'll be indexing a column whose data type is varchar(50). Would I be better off (better performance) if I changed the column's...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
4
by: Dwayne Epps | last post by:
I've created a function that checks form fields that only will have letters. This is the script: <script type="text/javascript" language="javascript"> function validateString(field, msg, min,...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
0
by: sfh | last post by:
Greetings all, I have a question concerning primary key types. In the past, I have always created tables with a primary key as an "int" such as: CREATE TABLE color_id ( color_id int(10)...
10
by: Techie | last post by:
what's the difference of the two data types?
7
by: James o'konnor | last post by:
hello. i have the next for create one table into db2 CREATE TABLE "MYSQUEMA"."TABLADEMO" ( "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH +0 INCREMENT BY +1 MINVALUE +0...
10
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
0
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...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.