[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL - Trim whitespaces & line breaks?

Posted on 2009-02-20
1
Medium Priority
?
1,378 Views
Last Modified: 2012-05-06
First, is is possible to trim line breaks from fields in my table?
Second, if I use trim, will it remove *all* the whitespace at the end of a field?  In some cases, it looks like the data has multiple spaces at the end, and I want to be certain that I won't have to run TRIM repeatedly?
0
Comment
Question by:n00b0101
1 Comment
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 23696664
Yes, you can trim line breaks.

No, trim() will not remove all whitespace. It will only remove one type of whitespace at a time: either spaces or line breaks. It also differs between unix newline and windows crlf.

The syntax for trim() is:

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr  FROM] str)

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_trim

You can do any of these:

TRIM(TRAILING '\n' from column)  
TRIM(TRAILING '\r\n' from column)
TRIM(TRAILING ' ' from column)

...but you can't do them all in combination, as far as I know. You would have to run them all repeatedly, untill all whitespace is gone. It might be more efficient to use a script.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question