Solved

White space issue with SQL concatenate statement

Posted on 2008-10-15
2
808 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
2 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 125 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now