Solved

Trim a column in a SQL statement

Posted on 2007-03-22
3
4,125 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
  • 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

679 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