Solved

Trim a column in a SQL statement

Posted on 2007-03-22
3
4,126 Views
Last Modified: 2007-12-19
I am trying to write a SQL statement that will trim the last characters after a whitespace in one of the columns called Postcode. Any ideas how I do this. Here is the code so far
SELECT blabla.`User`.UserName as USER, blabla.`User`.Postcode AS pcode, blabla.`User`.Tel, blabla.`User`.Email, blabla.`User`.Address, blabla.Customer.CustomerID FROM blabla.Customer, blabla.`User` WHERE blabla.Customer.UserID = blabla.`User`.UserID AND blabla.`User`.Postcode IS NOT NULL"
Customer has a PK CustomerID and a FK UserID. User has a PK UserID
The data is from a MySQL database
Seb
0
Comment
Question by:sebastiz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:Ved Prakash Agrawal
ID: 18769753
you can use RIGHT Function.
0
 
LVL 14

Accepted Solution

by:
Renante Entera earned 500 total points
ID: 18769758
Hi sebastiz!

I think this is what you are looking for :
===========================================
SELECT
  blabla.`User`.UserName as USER,
  blabla.`User`.Postcode AS pcode_orig,
  LEFT(blabla.`User`.Postcode, LENGTH(blabla.`User`.Postcode) - LOCATE(' ', REVERSE(blabla.`User`.Postcode))) as pcode_trim,
  blabla.`User`.Tel, blabla.`User`.Email, blabla.`User`.Address, blabla.Customer.CustomerID
FROM blabla.Customer, blabla.`User`
WHERE blabla.Customer.UserID = blabla.`User`.UserID
AND blabla.`User`.Postcode IS NOT NULL"
===========================================

So, the logic there is that you will locate first the position of the first occurence of "white space" but be sure to reverse the actual string.  Once you got the position then that would be deducted to the length of the actual string.  So, you will simply use function LEFT() to comeup with the expected value.

I hope that this helps you.  Just try it.


Goodluck!
eNTRANCE2002 :-)
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 18786940
Hi sebastiz!

I'm glad that I have helped you.  Hope to help you again...

BTW, thanks for the points and grade.


Regards!
eNTRANCE2002 :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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