Solved

Trim a column in a SQL statement

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

860 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