Solved

SQL - how to conditional add space when append a string

Posted on 2012-04-04
2
453 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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

831 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