?
Solved

SQL - how to conditional add space when append a string

Posted on 2012-04-04
2
Medium Priority
?
480 Views
Last Modified: 2012-04-04
I have
a.[Name] + ' ' + a.[MiddleName] + ' ' + a.[SurName] As Name

This will create a string with one spacing between words

However when a.[MiddleName] is empty, I will have two spaces between Name and SurName.  How can I structure the above so there will be only a single space if MiddleName is empty
0
Comment
Question by:tommym121
[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 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37806430
Please try:

a.[Name] + ' ' + (case when a.[MiddleName] = '' then '' else a.[MiddleName] + ' ' end) + a.[SurName] As Name
0
 

Author Closing Comment

by:tommym121
ID: 37806461
Thanks.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

764 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