Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trim a column in a SQL statement

Posted on 2007-03-22
3
Medium Priority
?
4,128 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 2000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

715 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