473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ 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 7424
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
22410
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 data type to some numeric type? I would have to update the column's data to accomodate this, but I would do it if this offers a performance gain. -- Bill
7
6189
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 make it better soon. Unfortunately, there is never a non-crucial time in which we can do an upgrade, so we are stuck for now. Point 1: There are multiple tables: students, courses, cross-reference
4
6116
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, max) { if (!min) { min = 1 } if (!max) { max = 65535} if (!field.value || field.value.length < min || field.value.max > max) { alert(msg); field.focus();
10
27874
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, obviously, just use LEN). My initial thought was to REVERSE it, find the location (using CHARINDEX) looking for each of those characters (so, multiple queries), then subtract that from the LEN of the string. The problem I'm running into is that...
0
2395
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) unsigned NOT NULL auto_increment primary key, color varchar(45) default NULL ) TYPE=INNODB DEFAULT CHARSET=latin1;
10
71052
by: Techie | last post by:
what's the difference of the two data types?
7
4763
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 MAXVALUE +2147483647 NO CYCLE
10
2997
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 the text and clicking a standard "submit" button. MAIN PROBLEM: My problem is that the information transmitted to the formhandler apparently has some sort of whitespace added to it. If I simply use trim() on the POST variable, it fails the...
0
27240
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 are constrained by the technology available at the time of their development and hence aren’t optimum as per current scenario. One of such cases is the extensive usage of CHAR fields, which aren’t optimum solution for space storage now. This paper...
0
7959
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8263
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...
1
8021
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6677
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...
1
5842
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
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
3876
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
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1492
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.