Solved

SQL - how to conditional add space when append a string

Posted on 2012-04-04
2
471 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 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.

737 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