Solved

White space issue with SQL concatenate statement

Posted on 2008-10-15
2
819 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

770 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