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

x
?
Solved

White space issue with SQL concatenate statement

Posted on 2008-10-15
2
Medium Priority
?
850 Views
Last Modified: 2012-05-05
Ok, hopefully this is an easy one for you. The following is a part of a SQL statement I am running.

LTRIM(RTRIM(LTRIM(RTRIM(ABCHCD) + ' ' + CAST(ABABTX AS VARCHAR(10)) + '' + RTRIM(ABACCD) + ' ' + RTRIM(ABAECD)+ ' ' + RTRIM(ABFXCD) + ' ' + RTRIM(ABDFCD))))


ABFXCD reperesents a field that does not always have a value. If ABDFCD ALSO does not have a value then I have no issues. However if ABDFCD does have a value there are two spaces before ABDFCD instead of one.

Example:

100 MORTIER DR  1001 (What I am getting)
100 MORTIER DR 1001  (What I want)

Any suggestions on how to alter the code to make this work?
0
Comment
Question by:CSTX_Analysts
[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 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 22723722
use ltrim and rtrim on the values like you did the first field.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22723784
Since you only want to concatenate field + ' ' when the length is >0...

case when datalength(ABCHCD)>0 then abac ABCHCD cd + ' ' else '' end +
case when datalength(cast(ABABTX as varchar(10)))>0 then cast(ABABTX as varchar(10)) + ' ' else '' end +
case when datalength(abaccd)>0 then abaccd + ' ' else '' end +
case when datalength(ABAECD)>0 then ABAECD + ' ' else '' end +
case when datalength(ABFXCD)>0 then ABFXCD + ' ' else '' end +
case when datalength(ABDFCD)>0 then ABDFCD + ' ' else '' end
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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